0

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);
            }

    }

}
Exorcismus
  • 2,243
  • 1
  • 35
  • 68
  • what web server are you using? – Shahe Dec 16 '13 at 12:01
  • java, using java ServerSocket – Exorcismus Dec 16 '13 at 12:05
  • what is the browser you are testing on? and what version? – Shahe Dec 16 '13 at 12:07
  • Google Chrome version 31.0.1650.63 m – Exorcismus Dec 16 '13 at 12:08
  • so let me get this straight. When you are calling this function, it is not calling the onopen function but it is calling the onmessage function?? and when you close the server the onclose is fired? – Shahe Dec 16 '13 at 12:11
  • when the function is called , onpen is not called , nevermind for onmessage function because the client will not be recieving data , and yes when I close the server (in java) the onclose is fired normally – Exorcismus Dec 16 '13 at 12:15
  • 1
    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 Dec 16 '13 at 12:17
  • please put the following code at the end of your javascript function, I will update your code in the post: `onerror = function (){alert('an error occured !');}` – Shahe Dec 16 '13 at 12:23

2 Answers2

0

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?

Community
  • 1
  • 1
Shahe
  • 964
  • 2
  • 13
  • 34
0

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

Exorcismus
  • 2,243
  • 1
  • 35
  • 68