简体   繁体   中英

using Websocket api in Tabris.js to connect to socket.io server

I am trying to build a Tabris app for android that acts as a client for my socket.io server I was doing some reading and seen that the WebSocket API is supported in tabris.js so I try to connect to my socket.io server like this

var socket = new WebSocket('ws://159.89.92.113:4343');

but I get this error

The WebSocket protocol has too be a string or an array of strings

so then i tried to fill in that parameter with the only thing i could think of

var socket = new WebSocket('ws://159.89.92.113:4343', 'ws');

and then i get this error

Can not 'send' WebSocket message when WebSocket state is CONNECTING

i honestly dont know how to make this work i have tried alot of different things.

Regarding the protocols parameter, here's what the WHATWG Standard has to say:

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.

So refer to your server's implentation as to what to send for that parameter, if anything at all.


The "Can not 'send' WebSocket message when WebSocket state is CONNECTING" message shouldn't occur when connecting but very well could occur if you tried to send too soon; the message specifically occurs when send() is called during the connecting phase. You can delay the send message until the connection is establashed as so:

const socket = new WebSocket('ws://159.89.92.113:4343'); 
socket.onopen = (event) => {
    socket.send('Hello World');
};

Finally, here's a working end-to-end example of a WebSocket setup using a Tabris.js app as the client client with a websocket server:

https://github.com/eclipsesource/tabris-js/tree/2.x/examples/web-socket

Simply:

git clone https://github.com/eclipsesource/tabris-js
cd tabris-js
git checkout 2.x
cd examples/web-socket
npm install
tabris serve

and start the server in another window, from that same directory npm run server

*Note this was written for Tabris.js 2.x, so you'll either want to test it with a 2.x client or migrate it to 3.x .

ok so the problem the entire time was i needed to specify the protocol someone on github pointed out that they usualy use 'chat-protocol' for the parameter so once i did it worked perfectly

const socket = new WebSocket('ws://157.230.66.208:4343/socket.io/?EIO=3&transport=websocket','chat-protocol');

also to talk to socket.io with regular web sockets you have to format the string like so

socket.send('42' + JSON.stringify(['checkLogin' , username, password]));

worked like a charm just need to figure out how to get promises and calbacks to work like that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM