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.

Revisions

  1. Chronojam created this gist Jul 8, 2013.
    58 changes: 58 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    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();
    }