2015-08-25 206 views
0

我在使用WebSocket4Net庫時遇到問題。Websocket4Net發送多條消息

我有事件websocket_opened並且當打開時引發此事件,如果我不發送任何消息我有一個例外

的System.Exception:後的WebSocket是 打開後,您必須通過WebSocket的發送數據

發送此消息後,我無法發送任何其他消息。我執行發送命令,但我沒有任何例外,它不工作,如果我檢查狀態websocket打開

如果我關閉打開的事件上的套接字,並再次打開它在closed_event我可以發送另一個消息沒有問題。

因此,如果我想發送多條消息,則必須在發送消息後重新連接併發送下一條消息才能斷開連接。

private static void websocket_Opened(object sender, EventArgs e) 
{ 
    websocket.Send(msg); 
    websocket.Close(); 
} 

private static void websocket_Closed(object sender, EventArgs e) 
{ 
    websocket.Open(); 
} 

這是正常的嗎?你能幫我解決這個問題嗎?

+0

這聽起來不對。你使用它作爲客戶端還是服務器? – vtortola

回答

-1

我認爲這將解決您的問題

websocket.Open(); 
private static void websocket_Opened(object sender, EventArgs e) 
{ 
    websocket.Send(msg); 
    websocket.Close(); 
} 

private static void websocket_Closed(object sender, EventArgs e) 
{ 
    //DO SOMETHINGS 
} 
1

我有一個類似的問題,當我第一次使用WebSocket4Net。 這個庫有一個好處,它有一個repository on github與許多examples of use (tests)。這些測試對我瞭解應該如何工作非常有幫助。

更新: 我看到弗雷德的評論如下,我想我真的需要添加一個例子。 所以,這是它,我用this api

using SuperSocket.ClientEngine; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 
using WebSocket4Net; 

namespace TestProj 
{ 
    public class WebSocketTest 
    { 
    static string webSocketUri = "wss://ws.binaryws.com/websockets/v3?app_id=1089"; 

    static void Main(string[] args) 
    { 
     var socketManager = new WebSocketManager(webSocketUri); 

     socketManager.Send("{\"time\": 1}"); 
     socketManager.Send("{\"ping\": 1}"); 
     socketManager.Close(); 

     Console.WriteLine("Console can be closed..."); 
     Console.ReadLine(); 
    } 
} 
public class WebSocketManager 
{ 
    private AutoResetEvent messageReceiveEvent = new AutoResetEvent(false); 
    private string lastMessageReceived; 
    private WebSocket webSocket; 

    public WebSocketManager(string webSocketUri) 
    { 
     Console.WriteLine("Initializing websocket. Uri: " + webSocketUri); 
     webSocket = new WebSocket(webSocketUri); 
     webSocket.Opened += new EventHandler(websocket_Opened); 
     webSocket.Closed += new EventHandler(websocket_Closed); 
     webSocket.Error += new EventHandler<ErrorEventArgs>(websocket_Error); 
     webSocket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(websocket_MessageReceived); 

     webSocket.Open(); 
     while (webSocket.State == WebSocketState.Connecting) { }; // by default webSocket4Net has AutoSendPing=true, 
                    // so we need to wait until connection established 
     if (webSocket.State != WebSocketState.Open) 
     { 
      throw new Exception("Connection is not opened."); 
     } 
    } 

    public string Send(string data) 
    { 
     Console.WriteLine("Client wants to send data:"); 
     Console.WriteLine(data); 

     webSocket.Send(data); 
     if (!messageReceiveEvent.WaitOne(5000))       // waiting for the response with 5 secs timeout 
      Console.WriteLine("Cannot receive the response. Timeout."); 

     return lastMessageReceived; 
    } 

    public void Close() 
    { 
     Console.WriteLine("Closing websocket..."); 
     webSocket.Close(); 
    } 

    private void websocket_Opened(object sender, EventArgs e) 
    { 
     Console.WriteLine("Websocket is opened."); 
    } 
    private void websocket_Error(object sender, ErrorEventArgs e) 
    { 
     Console.WriteLine(e.Exception.Message); 
    } 
    private void websocket_Closed(object sender, EventArgs e) 
    { 
     Console.WriteLine("Websocket is closed."); 
    } 

    private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e) 
    { 
     Console.WriteLine("Message received: " + e.Message); 
     lastMessageReceived = e.Message; 
     messageReceiveEvent.Set(); 
    } 
    } 
} 
+0

最好從所提供的鏈接中引用您發現對問題有幫助的特定部分,並且還提供顯示_「應該如何工作」的代碼示例_。如果沒有這些信息,你的回答只能告訴讀者在別處尋找,並在很大程度上做他們自己的研究。 –