Created
July 8, 2013 01:05
-
-
Save Chronojam/5945574 to your computer and use it in GitHub Desktop.
This file contains 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
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