1

I'm trying to establish a websocket connection between a chrome extension running the following code:

if('WebSocket' in window){
  connect('ws://localhost:8181/chat');
}

function connect(host) {
  var ws = new WebSocket(host);
  ws.onopen = function () {
    alert('connected');
  };

  ws.onmessage = function (evt) {  
    alert('reveived data:'+evt.data);
  };

  ws.onclose = function () {
    alert('socket closed');
  };
};

And a Java server.

import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ServerThread extends Thread {
    Socket socket = null;
    int NumberOfThread;
    Main Mainthread;

    public ServerThread(Socket socket) {
    super("ServerThread");
    this.socket = socket;
    }




    @Override
    public void run(){

        try {
            while(true){


            BufferedReader SS_BF = new BufferedReader(new             InputStreamReader(socket.getInputStream()));


            String temp = SS_BF.readLine();
            System.out.println(temp);



            }

       } catch (IOException ex) {

           suicide();
        }

}

public void suicide(){
    try {

        /*this.socket.shutdownInput();
        this.socket.shutdownOutput();

        this.socket.close();*/

        Mainthread.Coroner(NumberOfThread);

    } catch (IOException ex) {
        Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
    }
}
public void identify(int n, Main m)throws IOException{

    NumberOfThread=n;
    Mainthread=m;

}
public void Send(String s)throws IOException{
    PrintStream SSPS = new PrintStream(socket.getOutputStream());
    SSPS.println(s);
    System.out.println ("Sent text to CLIENT_" + NumberOfThread);
}
}

So i can establish a connection, I get as far as the handshake, and then nothing. Either the client hangs or disconnects but the onOpen event is never triggered.

I hope i have given enough information. Can anyone tell me how to establish a websocket connection from a client in a google chrome extension and java server running on the desktop?

Thanks in advance! Smollett.

user1803920
  • 96
  • 2
  • 6
  • 3
    Could you post the server code? Likely your server doesn't return the right handshake response if `onopen` is never triggered. – pimvdb Dec 28 '12 at 15:22
  • I have tryed many different servers, the one i made has nothing for handshake: it's too long hang on... – user1803920 Dec 28 '12 at 15:25
  • The problem is on the server; your JavaScript code works fine: http://jsfiddle.net/fyght/. Are you using a pre-existing a Java WebSocket server package, or writing your own? – apsillers Dec 28 '12 at 15:26
  • cool, i've added the server code to my post. can you recommend the source code of a working java websocket server? – user1803920 Dec 28 '12 at 15:28
  • I don't know Java very well, but it looks like you didn't include any handshaking code. Note that you need to implement that for WebSocket to work, as well as the encoding/deconding protocol for message frames. WebSocket is not a raw socket; it's mandatory to include the protocol details. – pimvdb Dec 28 '12 at 15:30
  • See http://stackoverflow.com/questions/4278456/websockets-production-ready-server-in-java and https://google.com/#q=java%20websocket%20server – apsillers Dec 28 '12 at 15:30

0 Answers0