I am writing a google chrome extension which will communicate with a server implemented on java and running on my machine.
I have the following code in background.html which is executed as soon as the extension opens:
var ws = new WebSocket("ws://localhost:49731");
console.log("Web Socket created with the state "+ ws.readyState);
try {
ws.onopen=function(){
alert("Connection opened");};
}
catch (err) {
console.log(err);
}
The following is the code for the java server:
try{
hearsay_socket = new ServerSocket(49731);
System.out.println("Waiting on Accept");
js_socket = hearsay_socket.accept();
System.out.println("Handling client at " +
js_socket.getInetAddress().getHostAddress() + " on port " +
js_socket.getPort());
out = new PrintWriter(js_socket.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(js_socket.getInputStream()));
System.out.println("Web socket Accepted");
for(;
{
bufferedData= in.readLine();
System.out.println(bufferedData);
if(bufferedData.length()==0)
{
System.out.println("Breaking");
break;
}
}
}
catch(UnknownHostException e)
{
System.err.println("Have no information about the host : localhost");
System.exit(1);
}
catch(IOException e)
{
System.err.println("Couldn't establish communication");
}
handShake1 = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"+
"Upgrade: WebSocket\r\n"+
"Connection: Upgrade\r\n";
handShake2= "WebSocket-Origin: chrome-extension://jikhaebpllfkfacdccmeonpkpfcnldna\r\n"+
"WebSocket-Location: ws:/"+js_socket.getLocalAddress()+":"+js_socket.getLoc alPort()+"\r\n\r\n";
handShake2=handShake2.toLowerCase();
System.out.println(handShake2);
byte[] bytes1=handShake1.getBytes("ASCII");
byte[] bytes2= handShake2.getBytes("ASCII");
out.print(bytes1);
out.print(bytes2);
out.close();
in.close();
hearsay_socket.close();
js_socket.close();
}
}
I read almost all the documentations of websockets and websocket protocol but i did not find any glitch. Could anyone please help??
Its urgent.
P.S. : Also i am able to come out of accept() on the server side in java, so it atleast accepts the first part of the web protocol handshake, but somehow the second part i.e from server to the browser is not complete.


LinkBack URL
About LinkBacks



Reply With Quote