WebSockets,GlassFish,Grizzly - 无法连接 [英] WebSockets, GlassFish, Grizzly -- can't connect

查看:308
本文介绍了WebSockets,GlassFish,Grizzly - 无法连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开始使用WebSockets,并尝试编写一个简单的应用程序来通过websoket来回发送消息。

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?

以下是我的WebSockets类的代码。当调用 .onConnect()时,它会记录:

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


我是套接字,我有联系。我联系了吗? - false

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

更新:在JavaScript中,我创建了有问题的套接字, readyState 1 ,这意味着套接字打开,可以进行通信。

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


推荐答案

如果你只是想在某个地方建立连接,你可能想尝试这样做。有一个实时工作演示,你可以下载javascript代码并自己玩。请注意,javascript代码仅在您安装在服务器上时才起作用(由于浏览器的安全性,因为它很花哨。)还有一步一步的基于浏览器的客户端教程正在我将发布的工作中尽快发布准备。大多数代理服务器尚未升级为处理websockets,因此它们会搞砸连接请求,大多数人无法连接到websocket服务器。 Firefox 7(发布)或谷歌浏览器14或更高版本支持演示服务器运行的最新版本的websocket协议。

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.

如果您想尝试获得灰熊演示工作,你可能要做一些调试,也许我会帮忙。请注意,在文章下面的评论中,其他人说他们无法正常工作要么我也没有找到任何后续行动。在这一点上,它似乎并不比上面的echo应用程序更好,即使我们确实让它运行,并且可能过于复杂并且如果你只是想要开始的话,那就是文档不足。但是如果你想尝试让它运行,你应该'git'代码的最新版本这里,最近至少已提交并可能已修复。

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.

然后确保应用程序javascript文件中的app.url为设置为您的安装目录。他的硬编码为:

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',

如果您使用的是Firefox 7,则需要修改javascript以使用Moz前缀,例如:

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.

HLL websocket服务器演示代码全部整理出来。

(另一个)更新:当我工作通过我自己的灰熊,我在glassfish管理控制台的快速入门中找到了一个很容易设置和运行的hello示例。你会在那里找到指示。示例目录还包含一个名为:websocket-mozilla的war文件;所以我猜它应该使用websockets。熟悉jsp的人应该查看源代码。我只能看到它正在使用http会话。根本没有提到websocket。这很像你好的样本。

(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.

这篇关于WebSockets,GlassFish,Grizzly - 无法连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆