在 Tabris.js 中使用 Websocket api 连接到 socket.io 服务器
using Websocket api in Tabris.js to connect to socket.io server
我正在尝试为 android 构建一个 Tabris 应用程序,作为我的 socket.io 服务器的客户端 我正在阅读一些资料,发现支持 WebSocket API tabris.js 所以我尝试像这样连接到我的 socket.io 服务器
var socket = new WebSocket('ws://159.89.92.113:4343');
但是我得到这个错误
The WebSocket protocol has too be a string or an array of strings
所以我试着用我唯一能想到的东西来填充那个参数
var socket = new WebSocket('ws://159.89.92.113:4343', 'ws');
然后我得到这个错误
Can not 'send' WebSocket message when WebSocket state is CONNECTING
老实说,我不知道如何完成这项工作我已经尝试了很多不同的东西。
关于protocols
参数,WHATWG Standard是这样说的:
protocols is either a string or an array of strings. If it is a string, it is equivalent to an array consisting of just that string; if it is omitted, it is equivalent to the empty array. Each string in the array is a subprotocol name. The connection will only be established if the server reports that it has selected one of these subprotocols. The subprotocol names have to match the requirements for elements that comprise the value of Sec-WebSocket-Protocol
fields as defined by The WebSocket protocol.
因此,请参考您的服务器的实现,了解为该参数发送什么(如果有的话)。
"Can not 'send' WebSocket message when WebSocket state is CONNECTING" 消息不应在 连接 时出现,但如果您尝试过早发送,则很可能会出现; the message 特别在连接阶段调用 send()
时发生。您可以延迟发送消息,直到建立连接为止:
const socket = new WebSocket('ws://159.89.92.113:4343');
socket.onopen = (event) => {
socket.send('Hello World');
};
最后,这是一个使用 Tabris.js 应用程序作为带有 websocket 服务器的客户端客户端的 WebSocket 设置的端到端工作示例:
https://github.com/eclipsesource/tabris-js/tree/2.x/examples/web-socket
简单地说:
git clone https://github.com/eclipsesource/tabris-js
cd tabris-js
git checkout 2.x
cd examples/web-socket
npm install
tabris serve
并从同一目录 npm run server
在另一个 window 中启动服务器
*请注意,这是为 Tabris.js 2.x 编写的,因此您需要使用 2.x 客户端或 migrate it to 3.x.[=21= 来测试它]
好的,所以问题一直是我需要指定协议 github 上的某个人指出他们通常使用 'chat-protocol' 作为参数,所以一旦我做到了,它就完美地工作了
const socket = new WebSocket('ws://157.230.66.208:4343/socket.io/?EIO=3&transport=websocket','chat-protocol');
还要使用常规网络套接字与 socket.io 通信,您必须像这样格式化字符串
socket.send('42' + JSON.stringify(['checkLogin' , username, password]));
像魅力一样工作只是需要弄清楚如何让承诺和回调像那样工作
我正在尝试为 android 构建一个 Tabris 应用程序,作为我的 socket.io 服务器的客户端 我正在阅读一些资料,发现支持 WebSocket API tabris.js 所以我尝试像这样连接到我的 socket.io 服务器
var socket = new WebSocket('ws://159.89.92.113:4343');
但是我得到这个错误
The WebSocket protocol has too be a string or an array of strings
所以我试着用我唯一能想到的东西来填充那个参数
var socket = new WebSocket('ws://159.89.92.113:4343', 'ws');
然后我得到这个错误
Can not 'send' WebSocket message when WebSocket state is CONNECTING
老实说,我不知道如何完成这项工作我已经尝试了很多不同的东西。
关于protocols
参数,WHATWG Standard是这样说的:
protocols is either a string or an array of strings. If it is a string, it is equivalent to an array consisting of just that string; if it is omitted, it is equivalent to the empty array. Each string in the array is a subprotocol name. The connection will only be established if the server reports that it has selected one of these subprotocols. The subprotocol names have to match the requirements for elements that comprise the value of
Sec-WebSocket-Protocol
fields as defined by The WebSocket protocol.
因此,请参考您的服务器的实现,了解为该参数发送什么(如果有的话)。
"Can not 'send' WebSocket message when WebSocket state is CONNECTING" 消息不应在 连接 时出现,但如果您尝试过早发送,则很可能会出现; the message 特别在连接阶段调用 send()
时发生。您可以延迟发送消息,直到建立连接为止:
const socket = new WebSocket('ws://159.89.92.113:4343');
socket.onopen = (event) => {
socket.send('Hello World');
};
最后,这是一个使用 Tabris.js 应用程序作为带有 websocket 服务器的客户端客户端的 WebSocket 设置的端到端工作示例:
https://github.com/eclipsesource/tabris-js/tree/2.x/examples/web-socket
简单地说:
git clone https://github.com/eclipsesource/tabris-js
cd tabris-js
git checkout 2.x
cd examples/web-socket
npm install
tabris serve
并从同一目录 npm run server
*请注意,这是为 Tabris.js 2.x 编写的,因此您需要使用 2.x 客户端或 migrate it to 3.x.[=21= 来测试它]
好的,所以问题一直是我需要指定协议 github 上的某个人指出他们通常使用 'chat-protocol' 作为参数,所以一旦我做到了,它就完美地工作了
const socket = new WebSocket('ws://157.230.66.208:4343/socket.io/?EIO=3&transport=websocket','chat-protocol');
还要使用常规网络套接字与 socket.io 通信,您必须像这样格式化字符串
socket.send('42' + JSON.stringify(['checkLogin' , username, password]));
像魅力一样工作只是需要弄清楚如何让承诺和回调像那样工作