简体   繁体   English

无法通过WebSocket连接进行连接

[英]Unable to connect via websocket connection

I'm trying to create a simple Websocket connection in my project. 我正在尝试在我的项目中创建一个简单的Websocket连接。

Java code: Java代码:

@ServerEndpoint("/echo")
public class EchoEndpoint {
@OnMessage
public void onMessage(Session session,String message){
    try{
        System.out.println(message);
    }
    catch(Exception e){
        System.out.println(e.getMessage());
    }
}

}

html and javascript code: html和javascript代码:

<button type="button" onclick="WebSocketTest()">Send</button>
<script type="text/javascript">
function WebSocketTest()
{

 alert("WebSocket is supported by your Browser!");
 // Let us open a web socket
 var ws = new WebSocket("ws://localhost:8080/echo");
 ws.onopen = function()
 {
    // Web Socket is connected, send data using send()
    ws.send("Message to send");
    alert("Message is sent...");
 };
 ws.onmessage = function (evt) 
 { 
    var received_msg = evt.data;
    alert("Message is received...");
 };
 ws.onclose = function()
 { 
    // websocket is closed.
    alert("Connection is closed..."); 
 };


 }
 </script>

after pressing the button I got the error WebSocket connection to 'ws://localhost:8080/echo' failed: Error during WebSocket handshake: Unexpected response code: 404 按下按钮后,我得到与WebSocket connection to 'ws://localhost:8080/echo' failed: Error during WebSocket handshake: Unexpected response code: 404的错误WebSocket connection to 'ws://localhost:8080/echo' failed: Error during WebSocket handshake: Unexpected response code: 404

Jboss Wildfly8 is used as Application Server. Jboss Wildfly8用作应用程序服务器。

Any Idea? 任何想法? or any working example? 或任何可行的例子?

This is because you put wrong path here: 这是因为您在此处输入了错误的路径:

var ws = new WebSocket("ws://localhost:8080/echo"); var ws = new WebSocket(“ ws:// localhost:8080 / echo”);

if your application is packed to eg: websocketapp.war (or if you set context-path on websocketapp) then you should use: 如果您的应用程序打包到例如:websocketapp.war(或者如果您在websocketapp上设置了上下文路径),则应使用:

var ws = new WebSocket("ws://localhost:8080/websocketapp/echo"); var ws = new WebSocket(“ ws:// localhost:8080 / websocketapp / echo”);

Connecting to web-socket For example 连接到网络套接字例如

var webSocket= new WebSocket("ws://l92.168.1.27:50333/project name//serverendpointdemo");

    var messagesTextArea=document.getElementsByClassId("messagesTextArea");
    webSocket.onopen=function(message){processOpen(message);};
    webSocket.onclose=function(message){processClose(message);};
    webSocket.onmessage=function(message){processMessage(message);};
    webSocket.onerror=function(message){processError(message);};
    function processOpen(message){
        messagesTextArea.value+="Server connected...."+"\n";
    }
    function processMessage(message){
        messagesTextArea.value+="Received from server:...."+message.data+"\n";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM