Skip to content

Instantly share code, notes, and snippets.

@MagnusThor
Created October 18, 2013 09:05
Show Gist options
  • Save MagnusThor/7038741 to your computer and use it in GitHub Desktop.
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
@{
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>
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
};
}
}
/// <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();
}
}
@danielwertheim
Copy link

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

@MagnusThor
Copy link
Author

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");

@WooCode
Copy link

WooCode commented Oct 18, 2013

Looks good :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment