为啥 JS WebSocket 在 sparkjava 中会卡在 CONNECTING 上?
Posted
技术标签:
【中文标题】为啥 JS WebSocket 在 sparkjava 中会卡在 CONNECTING 上?【英文标题】:Why does JS WebSocket get stuck on CONNECTING in sparkjava?为什么 JS WebSocket 在 sparkjava 中会卡在 CONNECTING 上? 【发布时间】:2021-02-10 04:11:43 【问题描述】:这是我的 websocket 代码:
@WebSocket
public class SocketServer
BetterLogger logger = new BetterLogger(Main.logger)
loggerName = "socket-server";
;
public static class ConnectionInit
String tty;
String device;
static class ConnectionOpenResponse
String tty;
@OnWebSocketConnect
public void onConnect(Session user) throws Exception
Main.logger.info("Websocket connected!");
@OnWebSocketClose
public void onClose(Session user, int statusCode, String reason)
Main.logger.info("Websocket disconnected!");
Main.sessions.closeSession(user);
@OnWebSocketMessage
public void onMessage(Session user, String message)
Main.logger.info("Recevied websock connection: " + message);
try
if (Main.sessions.contains(user))
// treat as raw buffer
Main.sessions.write(user, message);
else
ConnectionInit heartbeat = Main.gson.fromJson(message, ConnectionInit.class);
String s = Main.config.getTTY(heartbeat.device + '.' + heartbeat.tty);
// make sure that the tty exists in config
if (s == null)
user.close(400, "Invalid device/tty!");
// get UUID
Main.sessions.newSession(s, user);
logger.info("created session (" + user + ") with dev " + heartbeat.device + ":" + heartbeat.tty);
user.getRemote().sendString(Main.gson.toJson(new ConnectionOpenResponse()
tty = s;
, ConnectionOpenResponse.class));
catch (SessionException e )
user.close(500, e.getMessage());
catch (JsonSyntaxException e)
user.close(400, e.getMessage());
catch (IOException e)
// wtf
我已经在我的Main
类中正确注册了它,当我尝试使用websocat
连接到它时,一切似乎都运行良好,我可以发送数据并且一切正常。但是,一旦我创建了一个网页,websocket 就永远不会打开:
console.log("ws://" + location.hostname + ":" + location.port + "/device");
const socket = new WebSocket("ws://" + location.hostname + ":" + location.port + "/device");
var term = new Terminal();
term.open(document.getElementById('terminal'));
term.write('Press enter to re-flush buffers\n');
//while(socket.readyState !== 1);
//console.log("connected!");
socket.send("testdata");
term.onData( (data) =>
console.log(data);
if (data.charCodeAt(0) == 13)
socket.send('\n');
socket.send(data);
);
// Listen for messages
socket.addEventListener('message', function (event)
term.write(event.data);
);
现在,代码打印出正确的 URL (ws://localhost:16838/device
),但是,它卡在 CONNECTING
,并引发以下错误(我预计这是因为它仍在连接):
Uncaught DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
查看我的服务器日志,我可以看到它确实打印了Websocket connected!
,这甚至没有任何意义。解决我的 JS websocket 客户端卡住的原因有什么帮助吗?
【问题讨论】:
【参考方案1】:原来你需要添加打开事件,否则它实际上并没有打开
【讨论】:
以上是关于为啥 JS WebSocket 在 sparkjava 中会卡在 CONNECTING 上?的主要内容,如果未能解决你的问题,请参考以下文章
为啥反应 JS 挂钩 websocket onmessage 重置状态?
为啥选择 XMPP 而 Node.js 和 WebSocket 都可用? [关闭]
为啥在导入套接字时会出现此错误:“TypeError: _websocket_js__WEBPACK_IMPORTED_MODULE_0__.default.send is not a functio