WebSocket失败:WebSocket握手期间出错:意外响应代码:403

时间:2016-01-14 14:59:50

标签: javascript java-ee websocket wildfly

我正在研究野生动物8.2。如果我运行WebSocket连接到

var wsUrl = 'ws://' + window.location.host 

var ws = new WebSocket(wsUrl + '/testwebservlet') 

在我的服务器上,我在控制台中有这个消息:

WebSocket connection to 'ws://localhost:8282/testwebservlet' failed:
    Error during WebSocket handshake: Unexpected response code: 403

我的xhtml代码如下!!

<!DOCTYPE html>
<html>
<head>

<meta charset="UTF-8" />
<title>WebSocket Client</title>
<script type="text/javascript">      
 var wsUrl = 'ws://' + window.location.host 
 var ws = new WebSocket(wsUrl + '/testwebservlet')

     ws.onopen = function()
     {
        alert("Web Socket is connected!!");                 
     };
     ws.onmessage = function (evt) 
     {                  
        var msg = evt.data;
        alert("Message received:" +  msg);
     };
     ws.onclose = function()
     { 
        alert("Connection is closed..."); 
     };

  </script>
</head>
<body>

</body>
</html>

java代码:

package testwebservlet;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.enterprise.concurrent.ManagedExecutorService;
import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.jboss.logging.Logger;

@ServerEndpoint("/testwebservlet")
public class WSEndpoint {
    Logger log = Logger.getLogger(this.getClass());

    @OnMessage
    public void receiveMessage(String message, Session session) {
        log.info("Received : "+ message + ", session:" + session.getId());
    }

    @OnOpen
    public void open(Session session) {
        log.info("Open session:" + session.getId());         
    }

    @OnClose
    public void close(Session session, CloseReason c) {
        log.info("Closing:" + session.getId());
    }
}

有什么建议吗?

0 个答案:

没有答案