Last active
March 9, 2016 22:29
-
-
Save NVentimiglia/d52bdf678f85a25b4036 to your computer and use it in GitHub Desktop.
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.Collections.Generic; | |
using System.Linq; | |
using System.Web.Mvc; | |
using Fleck; | |
namespace FleckDemo.Controllers | |
{ | |
public class WSDemo : IDisposable | |
{ | |
private WebSocketServer server; | |
public WSDemo() | |
{ | |
FleckLog.Level = LogLevel.Debug; | |
var allSockets = new List<IWebSocketConnection>(); | |
//IPAny | |
server = new WebSocketServer("ws://0.0.0.0:9999"); | |
server.Start(socket => | |
{ | |
socket.OnOpen = () => | |
{ | |
Console.WriteLine("Open!"); | |
allSockets.Add(socket); | |
}; | |
socket.OnClose = () => | |
{ | |
Console.WriteLine("Close!"); | |
allSockets.Remove(socket); | |
}; | |
socket.OnMessage = message => | |
{ | |
Console.WriteLine(message); | |
allSockets.ToList().ForEach(s => s.Send("Echo: " + message)); | |
}; | |
}); | |
} | |
public void Dispose() | |
{ | |
if (server != null) | |
{ | |
server.Dispose(); | |
} | |
} | |
} | |
public class HomeController : Controller | |
{ | |
public static WSDemo Serv; | |
public ActionResult Index() | |
{ | |
if (Serv != null) | |
{ | |
Serv = new WSDemo(); | |
} | |
ViewBag.Title = "Home Page"; | |
return View(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment