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