Skip to content

Instantly share code, notes, and snippets.

@XSockets
Forked from MagnusThor/Foo.cshtml
Last active December 26, 2015 04:59
Show Gist options
  • Save XSockets/7097752 to your computer and use it in GitHub Desktop.
Save XSockets/7097752 to your computer and use it in GitHub Desktop.
A preview of what to come! We will bring you realtime ASP.NET MVC Controllers with STATE and WebSockets for .NET 4.5 solutions. But you will still be able to communicate through XSockets to other/older implementations on .NET 4/Mono.
//The RealTime controller of XSockets.NET
public class Chat : XSocketController
{
public string Nickname { get; set; }
public void ChatMessage(string text)
{
this.SendToAll(new { text, date = DateTime.Now, nickName = this.Nickname },"onChatMessage");
}
}
@{
ViewBag.Title = "Index";
}
<h2>This is the RazorView for the RealtimeMVC Controller - XSocketsControllerBase<ChatController> </h2>
<p>This is a realtime ASP.NET MVC Controller</p>
<div>
<input /><span></span>
</div>
<div id="messages"></div>
<script>
var ws;
$(function () {
ws = new XSockets.WebSocket("ws://localhost:8860/ChatController");
ws.on(XSockets.Events.open, function (connection) {
//Connected, just set a dummy Nickname on our ChatController
ws.publish("set_Nickname", { value: 'Sidney' });
console.log("Connected to XSockets using native IIS WebSockets.", connection);
//Listen for the 'onChatMessage' event and output the data to the DOM
ws.on("onChatMessage", function (msg) {
$("<p>").text(msg.text).prependTo("#messages");
});
});
//When user hits enter send the message
$("input").on("keydown", function (evt) {
if (evt.keyCode === 13) {
ws.publish("chatMessage", { text: $(this).val() });
$(this).val('');
}
});
});
</script>
//We inherit the XSocketsControllerBase and specify the XSocketsController that will keep state in our MVC controller
public class ChatController : XSocketsControllerBase<Chat>
{
// Just return the View (client)
public ViewResult Index()
{
return View();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment