Created
July 20, 2011 13:18
-
-
Save tarnacious/1094930 to your computer and use it in GitHub Desktop.
Very basic long polling "chat" application using Manos de mono
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
| /* | |
| A very basic long polling "chat" application using Manos de Mono (https://github.com/jacksonh/manos) | |
| The entire project is just this server code, index.html and app.js. | |
| With manos installed it can be built: | |
| $ manos --build | |
| Which produces and single assembly which can be run: | |
| $ manos --server | |
| Running ManosChat.ManosChat on port 8080. | |
| */ | |
| using Manos; | |
| using System; | |
| using System.IO; | |
| using System.Threading; | |
| using System.Collections.Generic; | |
| namespace ManosChat { | |
| public class ManosChat : ManosApp { | |
| public List<IManosContext> _waiting; | |
| public ManosChat () | |
| { | |
| _waiting = new List<IManosContext>(); | |
| } | |
| [Post ("/send")] | |
| public void Send(IManosContext context) | |
| { | |
| var message = context.Request.PostData["message"]; | |
| foreach(var listener in _waiting) { | |
| try { | |
| listener.Response.End(message); | |
| } catch(Exception) { | |
| } | |
| } | |
| _waiting.Clear(); | |
| context.Response.End(); | |
| } | |
| [Post ("/wait")] | |
| public void Wait(IManosContext context) | |
| { | |
| _waiting.Add(context); | |
| } | |
| [Get("/")] | |
| public void Home(IManosContext context) | |
| { | |
| var content = File.ReadAllText("index.html"); | |
| context.Response.End(content); | |
| } | |
| [Get("/app.js")] | |
| public void Script(IManosContext context) | |
| { | |
| var content = File.ReadAllText("app.js"); | |
| context.Response.End(content); | |
| } | |
| } | |
| } |
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() { | |
| var showMessage = function(message) { | |
| $('#messages').prepend($('<li/>').append(message)); | |
| }; | |
| var getMessages = function() { | |
| $.ajax({ | |
| type: 'POST', | |
| url: '/wait', | |
| data: 'data=none', | |
| success: function(data) { | |
| showMessage(data); | |
| getMessages(); } | |
| }); | |
| }; | |
| var sendMessage = function(message) { | |
| $.ajax({ | |
| type: 'POST', | |
| url: '/send', | |
| data: 'message=' + message, | |
| success: function(data) { | |
| } | |
| }); | |
| } | |
| $('#send-message').click(function(e) { | |
| e.preventDefault(); | |
| var m = $('#message').val(); | |
| sendMessage(m); | |
| }); | |
| getMessages(); | |
| }); |
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
| <!DOCTYPE HTML> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Manos Chat</title> | |
| <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> | |
| <script type="text/javascript" src="/app.js"></script> | |
| </head> | |
| <body> | |
| <h1>Chat</h1> | |
| <input type="text" id="message" name="message" /> | |
| <input type="submit" id="send-message" name="Send" /> | |
| <ul id="messages"> | |
| </ul> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment