gpt4 book ai didi

sockets - Java 服务器中的安全 WebSocket (WSS) 和 javascript 中的客户端

转载 作者:行者123 更新时间:2023-12-02 05:29:02 26 4
gpt4 key购买 nike

WebSocketwss://localhost:8025/websockets/server 的连接失败:WebSocket 打开握手超时

请帮助我做错了什么!

<小时/>

服务器端代码:

启动服务器

Server server = new Server("localhost", 8025, "/websockets", WebSocketServerEndpoint.class);
server.start();

@ServerEndpoint(value = "/server")
public class WebSocketServerEndpoint {
@OnOpen
public void onOpen(Session session) throws Exception
{
Logger.write("communication_clientservice", "WebSocket >> Connected ... " + session.getId());
}

@OnMessage
public void onMessage(String message, Session session) throws IOException
{
Logger.write("communication_clientservice_websocket", "UI to Websocket >> command received at Websocket >> " + message);
}

@OnClose
public void onClose(Session session, CloseReason closeReason) throws IOException
{
Logger.write("communication_clientservice_websocket", "WebSocket >>"+ String.format("Session %s closed because of %s", session.getId(), closeReason));
session.close();
}
}
<小时/>

客户端客户端连接服务器

 var ws = new WebSocket("wss://localhost:8025/websockets/server");             
ws.maxTimeout = 5 * 1000;
ws.onopen = function()
{
// Web Socket is connected, send data using send()
ws.send("{\"from\":\"shabeer\"}");
//alert("Message is sent...");
var elem = document.getElementById("receivedmsg");
elem.innerHTML = "panic request send";
};

ws.onmessage = function (evt)
{
var received_msg = evt;
//alert("Message is received..." + received_msg);
var elem = document.getElementById("receivedmsg");
elem.innerHTML = "Response received : " + received_msg;
};

ws.onclose = function()
{
// websocket is closed.
///alert("Connection is closed...");
setTimeout(function(){WebSocketTest()}, 5000);
};
<小时/>

最佳答案

Web Socket 服务器默认以 ws 开头(像 HTTP 一样不安全)。如果您需要 SSL (WSS),则必须在服务器对象中显式设置。

如果您在客户端中使用以下 URL,它应该可以工作。

wss://localhost:8025/ to ws://localhost:8025/

关于sockets - Java 服务器中的安全 WebSocket (WSS) 和 javascript 中的客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43570030/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com