Created
October 18, 2013 09:05
-
-
Save MagnusThor/7038741 to your computer and use it in GitHub Desktop.
Sneak peak of how to run XSockets.NET on top of IIS8 and Windows 2012/Windows 8 using the built-in WebSockets Protocol - Using a MVC4+ View ,Razor. and the coming XSocketsControllerBase<IXBaseSocket> class. More info about XSockets.NET on http://xsockets.net
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@{ | |
ViewBag.Title = "Index"; | |
} | |
<h2>This is the RazorView for the RealtimeMVC Controller - XSocketsControllerBase<Foo> </h2> | |
<p>This is a realtime MVC Controller</p> | |
<div> | |
<input /><span></span> | |
</div> | |
<div id="messages"></div> | |
<script> | |
var ws; | |
$(function () { | |
ws = new XSockets.WebSocket("ws://localhost:8860/Foo"); | |
ws.on(XSockets.Events.open, function (connection) { | |
ws.publish("set_Nickname", { value: 'Magnus' }); | |
console.log("Connected to XSockets using native IIS WebSockets.", connection); | |
ws.on("onChatMessage", function (msg) { | |
$("<p>").text(msg.text).prependTo("#messages"); | |
}); | |
ws.on(XSockets.Events.onError, function (err) { | |
console.log("error", err); | |
}); | |
ws.on("onTimerElapsed", function (data) { | |
$("span").text(data.date); | |
}); | |
}); | |
$("input").on("keydown", function (evt) { | |
if (evt.keyCode === 13) { | |
ws.emit("chatMessage", { text: $(this).val() }); | |
$(this).val(''); | |
} | |
}); | |
}); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FooController : XSocketsControllerBase<Foo> | |
{ | |
// Just return the View (client) | |
public ViewResult Index() | |
{ | |
return View(); | |
} | |
public JsonResult IvoryBelongsToElephants() | |
{ | |
return new JsonResult() | |
{ | |
Data = "Ivory Belongs To Elephants", | |
JsonRequestBehavior = JsonRequestBehavior.AllowGet | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// XSockets Controller (Realtime ) | |
/// </summary> | |
public class Foo : XSocketController | |
{ | |
public string Nickname { get; set; } | |
public void ChatMessage(string text) | |
{ | |
this.SendToAll(new { text, date = DateTime.Now, nickName = this.Nickname }.AsTextArgs("onChatMessage")); | |
} | |
public Foo() | |
{ | |
this.OnOpen += Foo_OnOpen; | |
} | |
void Foo_OnOpen(object sender, OnClientConnectArgs e) | |
{ | |
var timer = new Timer(new TimeSpan(0, 0, 5).TotalMilliseconds); | |
timer.Elapsed += (o, args) => this.Send(new { date = DateTime.Now, test = "Hello from state full controller" }, "onTimerElapsed"); | |
timer.Start(); | |
} | |
} |
Thanks Daniel, yes , each instance of Foo will have its own session. Foo will act just as a "regular" XSockets Controller. Foo will know of the actual HttpRequest / Session
Regarding SendToAll , ,just the way i wrote it :-)
You can also of'coz do -
this.SendToAll(new { text, date = DateTime.Now, nickName = this.Nickname },"onChatMessage");
Looks good :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks clean.
Will each session have its own instance of Foo, since you can make use of state/properties in it?
Why does SendAll use AsTextArgs while send don't? They both seem to consume it as an object in the client?
Cheers,
//Daniel