1

I'm writing a websockets-client. I have two problems :

  1. When I close a window of my application a server goes down

  2. 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".

wonea
  • 4,783
  • 17
  • 86
  • 139
Serg
  • 85
  • 1
  • 12
  • Please UP the topic :( – Serg Oct 31 '14 at 04:48
  • The error hints at a timing issue, are you sure the socket is connected before the data is being sent? Bad practice is to assume the connection is connected, check the state before sending the data. – Wranorn Oct 31 '14 at 05:37
  • @Wranorn, look on the updated code. Client socket freezing on "Connecting" state. – Serg Oct 31 '14 at 06:34
  • can you connect anything from the client to the server (i.e. does telnet connect to the server?) – Wranorn Oct 31 '14 at 16:20
  • @Wranorn, Telnet connects to server but not sending the message. I have a small client on js and it works perfect. – Serg Oct 31 '14 at 22:43

1 Answers1

1

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;
        }
daviddv
  • 177
  • 7