Dart, how to listen for websocket close on server side? - events

I see that client side websockets have an onlClose stream which is useful, but why isn't there a complimentary onclose stream on the server side websocket? I would like to be able to clean up my websessions on the server side when the web socket closes but Im not sure how to detect that event occuring on the server side.

The server-side WebSocket API is a Stream. That means that to listen on data, you can do the following:
websocket.listen((data) {
// Do something with data.
}, onDone: () {
// No more data - read-direction was closed.
});
Where the onDone callback is invoked, when the WebSocket is closed.

Related

socket.io to talk to server that is implementing RFC6455 in a strict fashion

I am trying to make my socket.io javascript client talk to a server implemented in cpp using websocketpp and its not working. Its surprising that I cant configure socket.io to fall back to real websockets when I need them.
Any one has any ideas or suggestions on this ? going back to websocket npm and re implementing my client is the only way ?
I tried this, but it does not work
var socket = io.connect('http://localhost:8080', {
transports: [
'websocket',
'polling'
]
});
socket.io is an additional protocol on top of webSocket so a socket.io client can ONLY talk to a socket.io server. While socket.io uses webSocket for the transport, it needs support for it's additional layer on top of webSocket to work properly.
If you want to talk to a plain webSocket server, then you should use a plain webSocket client.
var socket = new WebSocket('ws://localhost:8080');
// Connection opened
socket.addEventListener('open', function (event) {
socket.send('Hello Server!');
});
// Listen for messages
socket.addEventListener('message', function (event) {
console.log('Message from server', event.data);
});
You can probably find a socket.io server modules for cpp if you'd like to fix the server-side of things to talk socket.io.

How to configure my sails app with https socket connection?

I have a sails backend API. And I already have an https connection.
(https://myapp.com/api)
How do I connect to a sails socket with my client side (Android and iOS) having an https connection? I dont have any problem connecting to a non HTTPS server. Hope there is a help.
all you have to do is open config/bootstrap.js, and made following changes there
module.exports.bootstrap = function(cb) {
// handle connect socket first event executes after logged in
sails.io.on('connect', function (socket){
socket.emit("connected",{ data: "here am i!" })
});
// handle custom listener for other stuff
sails.io.on('ping', function (socket){
socket.emit("pong",{ data: "send to android/ios/web client" })
});
cb();
};
here you can listen for multiple events as well as you can emit multiple private/broadcast messages as well and also all socket.io listeners will work here

socket.io frames data not readable in chrome devtools

Server :
function startServer() {
const io = new Server().attach(8090);
io.on('connection', (socket) => {
console.log("connection on server");
socket.emit('test','test data');
});
}
Client :
const socket = io(`${location.protocol}//${location.hostname}:8090`);
socket.on('test', (data) => {
console.log(data);
})
Then :
I start the server
I open the client code
Server log output :
connection on server
Client console output :
test data
So all looks good, but when I open the websocket panel on chrome devtools i see this :
I cannot find the websocket data in the frames : only the "probe" thing and numbers.
How can I see frames data ?
Using https://www.websocket.org/echo.html I can perfectly read data in the frames :
The 2probe and random numbers you are seeing in the Frames panel is the Packet Type, followed by a ping/pong between client and server. You can read the details here.
You can only view data in the Frames panel if you are broadcasting messages from the server. In your case, you are sending a message with data to the client connected and handling it in a callback. If you want both yourself and all other clients to see the data, use:
io.sockets.emit('test', 'test data');
If two client connections are made to the server, both will receive test data, when the event is handled with the test event on the client. When the second client connects, you will see the message in the Frames panel. You don't see it for your own connection.
If you want to only broadcast that message to all other client, but not yourself, use:
socket.broadcast.emit('test','test data');
You can see some more information in chrome://net-internals/, indicating the data being sent, though it doesn't show you the actual text content unless you have a network sniffing tool like Wireshark or Fiddler.

SocketIO - Does a browser refresh cause "on disconnect" to fire?

I'm using SocketIO and I need to disconnect a socket on the server side if the user presses refresh. I've tried implementing this like so:
io.on('disconnect', function(socket) {
socket.close();
})
But the disconnect event isn't firing. Is this intended behaviour and if so is there any way to disconnect a socket when a page is refreshed?
Yes, when implemented properly a browser refresh causes a disconnect event on the server. I just confirmed this with a simple test app.
The disconnect event is a socket event, not an io event. So, you need to register for it on the socket, not at the io level.
This is my event handler that works just fine on my test server:
io.on('connection', function(socket) {
socket.on('disconnect', function() {
console.log("disconnect: ", socket.id);
});
});
FYI, you do not need to close the socket yourself. That will happen automatically as all webSocket/socket.io connections are automatically closed whenever a page is refreshed or navigated away from.

Websocket closing issue

I am working on web application for client side which is user websocket connection to send/receive data.
Here is my problem.
If some parameter change on client side, I want to close my web socket connection and initiate new connection with new paramaeters.
I tried to close web socket connection bu using close(). But when I checked my browser console, websocket status is always 2.
Please help me to resolve this.
I am not completely sure of what problem you have, but if you set a handler for onclose, it will be triggered when you call close.
var ws = new WebSocket('wss://wss://echo.websocket.org');
ws.onclose=function(){
alert('The websocket just closed, readyState: ' + ws.readyState);
}
Demo: http://jsfiddle.net/6kh20bdy/
ReadyState 3 means closed: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#Constants

Resources