Vala/Libsoup WebSocket客户端连接Revolt卡在101状态码无Ready事件
使用Vala连接Revolt WebSocket时未收到Ready事件的问题
我尝试用Workbench提供的Vala示例连接聊天应用Revolt的WebSocket,但认证后始终没收到预期的Ready事件。根据Revolt官方文档,认证完成后服务器应该先返回Authenticated事件,再发送包含有效数据的Ready事件。我已经确认HTTP状态码返回了101 Switching Protocols,但不知道该怎么进一步排查问题。Workbench的示例代码在它自带的WebSocket服务上能正常运行,我是Vala和网络栈的新手,找不到相关的Vala示例参考。
以下是我目前的代码:
class WebsocketClient : Object { private Soup.Session session; private Soup.WebsocketConnection connection; private string user_token; public WebsocketClient (Soup.Session global_session, string user_token) { this.session = global_session; this.user_token = user_token; Soup.Message request = new Soup.Message ("GET", @"wss://ws.revolt.chat?format=json&token=$(user_token)"); session.websocket_connect_async.begin (request, null, null, 1, null, (obj, res) => { try { connection = session.websocket_connect_async.end (res); } catch (Error error_thing) { error (error_thing.message); } connection.error.connect (on_error); connection.closed.connect (on_closed); connection.message.connect (on_message); // on_open (); }); } private void on_message (int type, Bytes msg) { if (type != Soup.WebsocketDataType.TEXT) return; string response = (string) msg.get_data (); message(response); } // other methods... }
我在另一个文件中创建了WebSocket实例。根据valadoc上的说明,我本以为协议切换会自动完成或可自行处理,但找不到Session.websocket_connect_finish方法。
如果服务器返回"101 Switching Protocols",则msg的状态码和响应头会更新,随后WebSocket握手完成。成功的话,[method@Session.websocket_connect_finish]会返回一个新的[class@WebsocketConnection];失败则返回错误。
内容的提问来源于stack exchange,提问作者user23570204
相关产品推荐
相关产品推荐

