Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket Server supports write? #186

Closed
nicholasareed opened this issue May 10, 2019 · 3 comments
Closed

Websocket Server supports write? #186

nicholasareed opened this issue May 10, 2019 · 3 comments

Comments

@nicholasareed
Copy link
Contributor

Documentation seems to show it is easy to have a WS server write a message to connected clients, but I can't get it to work in practice. I checked the source seems to show that only the Client supports write.

Am I missing something obvious?

import {Server} from "websocket"
let wsServer = new Server({});
wsServer.write("hello"); // # Error: "Break: ?: call write: no function!"
@wilberforce
Copy link
Contributor

@nicholasareed

Hi - had the same issue until I wokred out you need to call from the scope of the callback.

This is kind of hacky, as it only works for one client. I really needs to be a list of clients, but I've not has time to look at this as I think the connections are already managed by the listener, so i would be best to use that rather than maintaning a separate list...

import {
	Server as Ws
} from "websocket"

let ws = new Ws({
	port: 8081
});

let the_ws=null;
ws.callback = function (message, value, etc) {
	trace(JSON.stringify({
		message: message,
		value: value,
		etc: etc
	}), '\n');
	switch (message) {
		case 1:
			trace("main.js: socket connect.\n");
			break;

		case 2:
			trace("main.js: websocket handshake success\n");
			trace('sending:', JSON.stringify(model));
			this.write(JSON.stringify(model));
			the_ws=this;
			break;

		case 3:
			trace(`main.js: websocket message received: ${value}\n`);
			var update = JSON.parse(value);
			for (var o in update) {
				model[o] = update[o];
			}
			this.write(JSON.stringify(model));
			break;

		case 4:
			trace("main.js: websocket close\n");
			the_ws=null;
			break;
		case 5:
			trace("main.js: websocket sub-protocol\n");
			return 'esp';
			break;
	}
}


....

function send_to client(stuff) {
	if ( the_ws )
		the_ws.write(JSON.stringify(stuff));
}

or do the ws.write from in the callback.

@phoddie
Copy link
Collaborator

phoddie commented May 10, 2019

...until I worked out you need to call from the scope of the callback

You can call write any time after a connection has been established. The object you call write on must be a valid client connection

import {Server} from "websocket";
let wsServer = new Server({});
wsServer.write("hello"); // # Error: "Break: ?: call write: no function!

In this case wsServer is not a connection to a client, but the server itself. Each client instance first appears when the Client.connect (value of 1) message is received. Recall that a server may have multiple simultaneous connections; they cannot all be accessed as wsServer.

Note: this in the callback is a connection instance, not the server itself.

@wilberforce
Copy link
Contributor

@phoddie

I'm even more confused!

You can call write any time after a connection has been established. The object you call write on must be a valid client connection

write on which on object? I've used breakpoints and found the outer server does not have a write method - are you saying this changes?.

Perhaps an example that does a broadcast to more that on client would be useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants