0
votes

I'm sending a text through web socket to a Java server but the onpen function is never called this is the function am using for the client (WebSocketTest), and when I do close the server , the alert message of onclose functions is called properly

function WebSocketTest()
{
  if ("WebSocket" in window)
  {

     alert("WebSocket is supported by your Browser!");
     // Let us open a web socket
     var ws = new WebSocket("ws://localhost:4444");
     ws.onopen = function()
     {
        // Web Socket is connected, send data using send()
        ws.send("Message to send");
        alert("Message is sent...");
     };
     ws.onmessage = function (evt) 
     { 
        var received_msg = evt.data;
        alert("Message is received...");
     };
     ws.onclose = function()
     { 
        // websocket is closed.
        alert("Connection is closed..."); 
     };
  }
  else
  {
     // The browser doesn't support WebSocket
     alert("WebSocket NOT supported by your Browser!");
  }
}

this is what am recieving at the server

GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:4444
Origin: null
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: 4BAiV8AU80juonjYQw5V9g==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame
User-Agent: Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko)      Chrome/31.0.1650.63 Safari/537.36

this is the server side

public class InvoiceListener {

    private static BufferedReader in;
    private final static int port = 4444;
    private static ServerSocket listenSocket;
    private static Socket client;
    private static Invoice invoice;
    private static String info;

    public static void main(String[] args) throws IOException {

        PrintInvoice printer;
        ServerSocket listenSocket = new ServerSocket(port);


            System.out.println("Listening");
            client = listenSocket.accept();
            System.out.println("client connected !");
            in = new BufferedReader(new InputStreamReader(
                    client.getInputStream()));
            while ((info = in.readLine()) != null) {
                System.out.println(info);
            }

    }

}
2
what web server are you using? - Shahe
java, using java ServerSocket - Exorcismus
what is the browser you are testing on? and what version? - Shahe
Google Chrome version 31.0.1650.63 m - Exorcismus
can you update your post with some more code? I mean how are you receiving the request at the server (java), and when are you calling this function in your page? - Shahe

2 Answers

0
votes

You can't use ServerSocket with web socket, you need to have a web server that supports listening to web socket calls, you can check here for supported web servers.

Supported web socket java servers:

What popular webservers have support for HTML5 WebSocket?

0
votes

OK I found the solution and it appears that the date am receiving were actually due to the handshake protocol

GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:4444
Origin: null
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: 4BAiV8AU80juonjYQw5V9g==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame
User-Agent: Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko)

and I must send a response from the server to client again to start the connection , that's when Onopen function works