WebSockets, GlassFish, Grizzly — can't connect

天大地大妈咪最大 提交于 2019-12-08 14:37:35
Roger F. Gay

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!