WebsocketSharp事件未触发

时间:2017-06-10 07:57:49

标签: c# winforms websocket websocket-sharp

事件未在以下代码中触发:

    private WebSocketSharp.WebSocket client;

    private void GetWebsocketFeedMessages()
    {
        string host = "wss://ws-feed.gdax.com";
        client = new WebSocket(host);
        client.Connect();
        client.OnOpen += client_OnOpen;
        client.OnMessage += client_OnMessage;
    }

    void client_OnMessage(object sender, MessageEventArgs e)
    {
        string response = e.Data;
    }

    void client_OnOpen(object sender, EventArgs e)
    {            
        client.Send("{     \"type\": \"subscribe\",     \"product_ids\": [         \"ETH-USD\"     ] }");
    }    

我正在使用vs2012 framework 4.5和windows应用程序。但是无法在开放和消息事件中到达该行。不知道我犯了什么错误,有人可以提出建议吗?

1 个答案:

答案 0 :(得分:0)

首先,您应该设置事件并在调用connect方法之后,因为它同步工作。

private void GetWebsocketFeedMessages()
{
    string host = "wss://ws-feed.gdax.com";
    client = new WebSocket(host);
    client.OnOpen += client_OnOpen;
    client.OnMessage += client_OnMessage;
    client.Connect();
}