Skip to content

Instantly share code, notes, and snippets.

@NVentimiglia
Last active March 9, 2016 22:29
Show Gist options
  • Save NVentimiglia/d52bdf678f85a25b4036 to your computer and use it in GitHub Desktop.
Save NVentimiglia/d52bdf678f85a25b4036 to your computer and use it in GitHub Desktop.
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