Reputation: 85
I'm writing a websockets-client. I have two problems :
When I close a window of my application a server goes down
Server did not receiving messages but Client always receives a greeting message from server.
System.Exception : You must send data by websocket after websocket is opened
Client on C# (Websocket4Net lib)
private static void _clientSocket_Closed(object sender, EventArgs e)
{
if (_clientSocket.State == WebSocket4Net.WebSocketState.Open)
{
_clientSocket.Close("Closed by user");
}
}
public static void WebRequest(string url, dutyObject objToSend)
{
_clientSocket = new WebSocket(url);
_clientSocket.MessageReceived += _clientSocket_MessageReceived;
_clientSocket.DataReceived += _clientSocket_DataReceived;
_clientSocket.Closed += _clientSocket_Closed;
_clientSocket.Error += new EventHandler<SuperSocket.ClientEngine.ErrorEventArgs>(_clientSocket_Error);
_clientSocket.Open();
var jsonMessage = JsonSerializeHelper.Serialize(objToSend);
_clientSocket.Send(jsonMessage);
}
Server on php
class Server extends WebSocketServer
{
protected function serverCreated()
{
}
/**
* This is run when server is receiving data.
*/
protected function process($connected_user, $message)
{
$this->send($connected_user,"[+]".$message); //just echo reply
}
/**
* This is run when socket connection is established. Send a greeting message
*/
protected function connected($connected_user)
{
$welcome_message = 'Welcome to Service. Service works with JSON. Be careful!';
$this->send($connected_user, $welcome_message);
}
protected function closed($connected_user)
{
$this->stdout("User closed connection \n");
}
}
UPDATE on client.
while (_clientSocket.State != WebSocketState.Open)
{
if (_clientSocket.State == WebSocket4Net.WebSocketState.Open)
{
Console.WriteLine(_clientSocket.State);
_clientSocket.Send(ecn.GetBytes(jsonMessage), 0, ecn.GetBytes(jsonMessage).Length);
}
else
{
Console.WriteLine("E: " + _clientSocket.State);
//_clientSocket.Close();
}
}
And it permanent says "Connecting".
Upvotes: 1
Views: 1514
Reputation: 177
I suspect this is probably with an error with handshake - when looking at the code I saw that if no handshake was made this error is thrown
private bool EnsureWebSocketOpen()
{
if (!Handshaked)
{
OnError(new Exception(m_NotOpenSendingMessage));
return false;
}
return true;
}
Upvotes: 1