1

I am trying to get started with WebSockets, and trying to write a simple application to send messages back and forth via a websoket.

However, it looks like the socket that I am trying to create never gets connected. Why can that be?

Below is the code of my WebSockets class. When .onConnect() is called, it logs:

I am socket, I was connected. Am i connected? - false

Update: in JavaScript, where I create the socket in question, the readyState is 1, which means "socket open, communication is possble".

import a.b.Misc; //writes logs.

import com.sun.grizzly.websockets.BaseServerWebSocket;
import com.sun.grizzly.websockets.DataFrame;
import com.sun.grizzly.websockets.WebSocketListener;

public class ChatWebSocket_v2 extends BaseServerWebSocket {
    private String user;
    public ChatWebSocket_v2(WebSocketListener... listeners) {
        super(listeners);
    }
    public String getUser() {
        if (user == null) {
            Misc.print("User is null in ChatWebSocket");
            throw new NullPointerException("+=The user is null in chat web socket");
        }
        return user;
    }
    public void setUser(String user) {
        Misc.print("Just set user: " + user);
        this.user = user;
    }
    @Override
    public void onMessage(String message) {
        Misc.print(message +"\n");
    }
    @Override
    public void onMessage(byte[] message) {
        Misc.print(new String(message) +" << Bytes\n");
    }
    @Override 
    public void onConnect() {
        Misc.print("I am socket, i was connected. Am i connected? - " + this.isConnected());
    }
    @Override 
    public void onClose(DataFrame df) {
        Misc.print("I am socket, i was closed");
    }
}
pimvdb
  • 151,816
  • 78
  • 307
  • 352
Ibolit
  • 9,218
  • 7
  • 52
  • 96

1 Answers1

1

If you're just trying to make a connection somewhere, you might want to try this instead. There is a live working demo and you can download the javascript code and play with it yourself. Note that the javascript code only works if you have it installed on a server (due to browser security because it's 'fancy'.) There is also a step by step browser-based client tutorial in the works that I will post as soon as it's ready. Most proxy servers haven't been upgraded to handle websockets so they will screw up connection request and most people won't be able to connect to websocket servers from work. Firefox 7 (release) or Google Chrome 14 or later support the latest version of the websocket protocol that the demo server runs.

If you want to try to get the grizzly demo working, you might have some debugging to do and maybe I'll help with that. Note that in comments below the article, other people said they couldn't get it working either and I haven't found any follow up. At this point it seems no better than the echo app above even if we do get it running and is possibly overly complicated and underly documented if you're just trying to get started. But if you want to try to get it running, you should 'git' the latest version of the code here, which was at least committed recently and may be fixed.

Then make sure that app.url in the application javascript file is set to your installation directory. His is hard-coded as:

url: 'ws://localhost:8080/grizzly-websockets-chat/chat',

If you're using Firefox 7, the javascript needs to be modified to use the Moz prefix, for example:

  if (typeof MozWebSocket != "undefined") { // window.MozWebSocket or "MozWebSocket" in window
    ok
  } else if (window.WebSocket) {  // he uses  if ("WebSocket" in window)
    ok
  } else {
    do your print "browser doesn't support websockets"
  }
  .... then if the browser supports websockets
  websocket = new WebSocket(app.url); or
  websocket = new MozWebSocket(app.url); 
  // depending on which it is.

The HLL websocket server demo code has this all sorted out.

(another) UPDATE: As I work through grizzly myself, I found on the Quick Start in the glassfish admin console, there's a hello sample that's pretty easy to set up and run. You'll find instructions there. The sample directory also contains a war file named: websocket-mozilla; so I guess its supposed to use websockets. Someone who's familiar with jsp should review the source code. All I can see is that it's using an http session. No mention of a websocket at all. It's a lot like the hello sample.

Roger F. Gay
  • 1,830
  • 2
  • 20
  • 25
  • It is what I have been trying. That's the same code as mine, but I just threw away some unnecessary methods and added some of my own. I also created the application and the servlet that registers it. I didn't show them here, as it seems that they work, at least the .onConnect in this class gets called, so the socket does indeed get created. It's .onMessage that doesn't work... – Ibolit Oct 04 '11 at 23:04
  • I may get into that myself. I'd like for my server to talk to the grizzly server. Maybe today. RE: My comment. The sequence is to make the connection first. There's an upgraded handshake procedure that "client" and server must go through. The tutorial version has a method that creates the socket for the client, etc. (I just skimmed. I might go through in detail today.) Your version doesn't have that. – Roger F. Gay Oct 05 '11 at 07:30
  • Once I have grizzly up and running, I'll also use my client to contact it. http://highlevellogic.blogspot.com/2011/09/websocket-server-demonstration_26.html – Roger F. Gay Oct 05 '11 at 07:32
  • BTW: I've used Tomcat quite a bit but have never installed Apache. If you have any tips re: getting grizzly up and running along with the sample echo server perhaps we can help each other. – Roger F. Gay Oct 05 '11 at 07:48
  • You don't exactly install Grizzly. You should install GlassFish from Oracle's site, and Grizzly is just a package that you can find in GlassFish's lib folder. – Ibolit Oct 05 '11 at 08:50
  • OK. I downloaded the source a while back and it looks like it could work stand-alone. There's also instructions for doing it. But your way sounds easier. – Roger F. Gay Oct 05 '11 at 09:32
  • We'll have to confirm that it works. I haven't looked recently, but I think back when I looked, the dev version was needed. What version websockets does the Oracle download support? – Roger F. Gay Oct 05 '11 at 09:33
  • I've run the installation for 3.1.1 from here: http://glassfish.java.net/downloads/3.1.1-final.html – Roger F. Gay Oct 05 '11 at 09:59
  • Read the comments at the end of the article. Others were having problems getting the app described in the article to work. It didn't even work in the version that was supposed to be shipped. I found the code in the repository: http://java.net/projects/grizzly/sources/git/show/samples/websockets/chat/src/main/java/org/glassfish/grizzly/samples/websockets?rev=99ad7b0f96ee9a989485377b8c64cfd140bef1c8 – Roger F. Gay Oct 05 '11 at 11:17
  • Do you know how to check the code out? I have git and TortoiseSVN installed but I haven't used either in quite a while, not even for my own project. – Roger F. Gay Oct 05 '11 at 12:46
  • Got it. Very simple to "git" all of grizzly including the chat sample. It had been so long I just had to spend some time finding and reading a tutorial again. – Roger F. Gay Oct 06 '11 at 06:41