Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java WebSocket在打开连接时获得404_Java_Javascript_Jsp_Tomcat_Websocket - Fatal编程技术网

Java WebSocket在打开连接时获得404

Java WebSocket在打开连接时获得404,java,javascript,jsp,tomcat,websocket,Java,Javascript,Jsp,Tomcat,Websocket,我正在跑步: 雄猫7 Java版本1.8 Chrome浏览器版本41.0.2272.89 m 我试图让我的websocket工作,但由于某些原因,我在尝试访问套接字时得到了404(在chrome中) WebSocket connection to 'ws://localhost:8080/InfiniteCloud/WebSocketExchange' failed: Error during WebSocket handshake: Unexpected response code: 40

我正在跑步:

  • 雄猫7
  • Java版本1.8
  • Chrome浏览器版本41.0.2272.89 m
我试图让我的websocket工作,但由于某些原因,我在尝试访问套接字时得到了404(在chrome中)

WebSocket connection to 'ws://localhost:8080/InfiniteCloud/WebSocketExchange' failed: Error during WebSocket handshake: Unexpected response code: 404
错误发生在我尝试打开连接时。我在这个论坛上查阅了很多以前的问题,但没有一个有效。这是我的服务器端代码:

package websocket;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.websocket.server.ServerEndpoint;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;

import org.json.simple.JSONObject;

@ServerEndpoint( value = "/ClientDataExchange")
public class WebSocketExchange {

    static Set<Session> _users = Collections.synchronizedSet(new HashSet<Session>());

    @OnOpen
    public void handleOpen(Session userSession){
        _users.add(userSession);
    }

    @OnMessage
    public void handleMessage(String message, Session userSession){
        try{

            if(userSession.getUserProperties().get("username") == null){
                userSession.getUserProperties().put("username", message);
                userSession.getBasicRemote().sendText( newUserJSONResponse(message) );
            }
            System.out.println("Message: " + message);

        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @OnClose
    public void handleClose(Session userSession){
        _users.remove(userSession);
    }

    public String newUserJSONResponse(String username){
        JSONObject j = new JSONObject();
        j.put("ICmessage", "You are now connected as: " + username);

        return j.toJSONString();
    }
}

你的路径似乎不匹配。在您拥有的服务器上

var url = "ws://" + document.location.host + document.location.pathname + "WebSocketExchange";
@ServerEndpoint(value=“/ClientDataExchange”)

在你的客户身上

var url = "ws://" + document.location.host + document.location.pathname + "WebSocketExchange";

你的路径似乎不匹配。在服务器上有@ServerEndpoint(value=“/ClientDataExchange”),在客户端上有var url=“ws://”+document.location.host+document.location.pathname+“WebSocketExchange”;非常感谢。我不知道我是怎么错过的。回答这个问题,这样我就可以给你一个答案了!:)伟大的(创建了一个答案)