Skip to content

Instantly share code, notes, and snippets.

@Chronojam
Created July 8, 2013 01:05
Show Gist options
  • Save Chronojam/5945574 to your computer and use it in GitHub Desktop.
Save Chronojam/5945574 to your computer and use it in GitHub Desktop.
WebSocketServer _webSocket;
List<IWebSocketConnection> openConnections = new List<IWebSocketConnection>();
public int port;
// Use this for initialization
public JavascriptGameInterface()
{
//port = (int)UnityEngine.Random.Range(50000, 65000);
_webSocket = new WebSocketServer("ws://localhost:8182");
//Debug.Log(port);
_webSocket.Start(socket =>
{
socket.OnBinary = OnBinaryMessage;
socket.OnClose = () => OnClose(socket);
socket.OnError = OnError;
socket.OnMessage = msg => OnMessage(socket, msg);
socket.OnOpen = () => OnOpen(socket);
});
}
void OnBinaryMessage(byte[] bl)
{
throw new NotImplementedException();
}
void OnClose(IWebSocketConnection wsc)
{
openConnections.Remove(wsc);
}
void OnError(Exception error)
{
throw error;
}
void OnMessage(IWebSocketConnection wsc, string msg)
{
}
void OnOpen(IWebSocketConnection wsc)
{
openConnections.Add(wsc);
}
public void SendMessage(JObject jo)
{
string message = jo.ToString();
foreach (IWebSocketConnection wsc in openConnections)
{
wsc.Send(message);
}
}
public void Dispose()
{
_webSocket.Dispose();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment