node.js - can I have different websockets input in the same server - output in different clients -


let me clarify title of question.

on client side have 2 different html files, client1.html , client2.html. send data via websockets same server.js file, in node.js.

on server side, if data came client1.html want perform query , send outcome client1.html. on same server, if data came client2.html want perform query , send client2.html message "data saved".

i guess, have create 2 different functios in server.js. ok.

but, problem is, on server side, how tell, data came client? also, how server can send right message right client?

thanks in advance

you have register clients. example if user on page client1.html send message (via websockets) example json (or other format like):

{ "user": "a", "page": "client1.html" } 

now on server side mark user/connection came client1.html. can add example custom property:

conn.source = "client1.html"; 

or other way (depending example on framework).

you might use handshake (instead of sending json): when connecting server example (on client side):

var ws = new websocket("ws://myserver/client1.html"); 

now have same in handshake code (client1.html part of url in handshake).

as other question: on server side keep lists of users client1.html, client2.html, etc. rest obvious: loop on target list , send notification users.

of course there many small details here. have remove users lists if connection dead (thus need background task check whether connection alive), etc. that's general idea.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -