jWebsocket未连接错误

时间:2012-08-15 10:42:13

标签: java websocket

在我的jsp文件中,我有

  <script type="text/javascript" src="/resources/scripts/jWebSocket.js"></script>

  <script type="text/javascript">
  //jws.browserSupportsWebSockets checks if web sockets are available
  //either natively, by the FlashBridge or by the ChromeFrame.
  if( jws.browserSupportsWebSockets() ) {
  jWebSocketClient = new jws.jWebSocketJSONClient();
  // Optionally enable GUI controls here
  } else {
  // Optionally disable GUI controls here
  var lMsg = jws.MSG_WS_NOT_SUPPORTED;
  alert(lMsg);
 }

  var lURL = jws.getDefaultSSLServerURL() + "/;timeout=360000";
 alert(lURL);

     /* var lRes = jWebSocketClient.broadcastText(
    "",   // broadcast to all clients (not limited to a certain pool)
    lMsg  // broadcast this message
  );

  alert(lRes.code);
   */

  function broadcast() {

        var lMsg = "This is test message";
        var lTarget = "*";
        if( lMsg.length > 0 ) {
            //log( USR, OUT, lMsg );
            var lRes;
            if( !lTarget || lTarget == "*") {
                alert(lMsg);
                lRes = jWebSocketClient.broadcastText(
                    "",         // broadcast to all clients (not limited to a certain pool)
                    lMsg        // broadcast this message
                );
            } else {
                lRes = jWebSocketClient.sendText(
                    lTarget,    // broadcast to all clients (not limited to a certain pool)
                    lMsg        // broadcast this message
                );
            }
            // log error only, on success don't confuse the user
            if( lRes.code != 0 ) {
                alert(lRes.msg);
                //log( SYS, OUT, "broadcast: " + lRes.msg );
            }
            // eMessage.value = "";
        }
        //doFocus( eMessage );
    }     

   </script>

如果(jws.browserSupportsWebSockets())它不会转到其他部分。这意味着浏览器支持websockets。

最后它转到if(lRes.code!= 0)并警告“Not connected”

我错过了什么。请帮忙

在我的终端窗口中,我收到此消息

 TCPEngine: SSL engine 'tcp0' started' at port 9797 with default timeout infinite.
 2012-08-15 09:52:45,979 DEBUG - JWebSocketFactory: Starting servers...
 2012-08-15 09:52:45,980 INFO  - TokenServer: Token server 'ts0' started.
 2012-08-15 09:52:45,980 INFO  - JWebSocketFactory: jWebSocket server startup complete

表示服务器正在运行。

当我从下面的代码中提醒lURL时

 var lURL = jws.getDefaultSSLServerURL() + "/;timeout=360000"; in my jsp file.

打印

    wss://localhost:9797/jWebSocket/jWebSocket/;timeout=360000

1 个答案:

答案 0 :(得分:0)

是。您缺少登录呼叫。添加以下行(而不是alert(lUrl)所在的位置):

jWebSocketClient.logon( lURL, "guest", "guest", {

   OnOpen: function( aEvent ){
   // something to do when connection is established
   }

   OnMessage: function( aEvent, aToken ){
   // something to do when a message is recieved
   }
}

第一个"guest"是用户名,第二个是密码。如果你还没有更改服务器中的jwebsocket.conf文件,那么应该有一个拥有这些凭据的用户。

让我们知道这是否有帮助