Created
February 3, 2014 14:53
-
-
Save joeriks/8785228 to your computer and use it in GitHub Desktop.
SignalR and FnX.EsentStore
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
| $(function () { | |
| // Reference the auto-generated proxy for the hub. | |
| var chat = $.connection.chatHub; | |
| // Create a function that the hub can call back to display messages. | |
| function initialise(){ | |
| chat.server.getAll().done( | |
| function (messaages) { | |
| for (var msg in messaages) { | |
| $('#discussion').append('<li><strong>' + htmlEncode("foo") | |
| + '</strong>: ' + htmlEncode(messaages[msg]) + '</li>'); | |
| } | |
| } | |
| ); | |
| } | |
| chat.client.addNewMessageToPage = function (name, message) { | |
| // Add the message to the page. | |
| $('#discussion').append('<li><strong>' + htmlEncode(name) | |
| + '</strong>: ' + htmlEncode(message) + '</li>'); | |
| }; | |
| // Get the user name and store it to prepend to messages. | |
| $('#displayname').val(prompt('Enter your name:', '')); | |
| // Set initial focus to message input box. | |
| $('#message').focus(); | |
| // Start the connection. | |
| $.connection.hub.start().done(function () { | |
| $('#sendmessage').click(function () { | |
| // Call the Send method on the hub. | |
| chat.server.send($('#displayname').val(), $('#message').val()); | |
| // Clear text box and reset focus for next comment. | |
| $('#message').val('').focus(); | |
| }); | |
| initialise(); | |
| }); | |
| }); | |
| // This optional function html-encodes messages for display in the page. | |
| function htmlEncode(value) { | |
| var encodedValue = $('<div />').text(value).html(); | |
| return encodedValue; | |
| } |
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
| using System; | |
| using System.Web; | |
| using Microsoft.AspNet.SignalR; | |
| using System.Collections.Generic; | |
| namespace SignalRChat | |
| { | |
| public class ChatHub : Hub | |
| { | |
| public IEnumerable<string> GetAll() | |
| { | |
| var es = new FnX.EsentStore("foo"); | |
| var list = es.Get<List<string>>("list"); | |
| if (list.Value == null) list.Do(l => new List<string>()); | |
| return list.Value; | |
| } | |
| public void Send(string name, string message) | |
| { | |
| var es = new FnX.EsentStore("foo"); | |
| var list = es.Get<List<string>>("list"); | |
| if (list.Value == null) list.Value = new List<string>(); | |
| list.Do(t => t.Add(message)); | |
| Clients.All.addNewMessageToPage(name, message); | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment