javascript - Send data from websocket to socket.io -
i used websocket interface connect websocket server . if want send data receive websocket server through websocket interface client connected me through http server , should use socket.io ?
so @ end have socket.io attached to http server , websocket interface data , in case of message come send client through socket.io . best setup ?
code example :
// require http module (to start server) , socket.io var http = require('http'), io = require('socket.io'); var websocket = require('ws'); var ws = new websocket('ws://localhost:5000'); // start server @ port 8080 var server = http.createserver(function (req, res) { // send html headers , message res.writehead(200, { 'content-type': 'text/html' }); res.end('<h1>hello socket lover!</h1>'); }); server.listen(8080); // create socket.io instance, passing our server var socket = io.listen(server); ws.on('open', function open() { ws.send('something'); }); ws.on('message', function (data, flags) { // here data send socket.io }); // add connect listener socket.on('connection', function (client) { // success! listen messages received client.on('message', function (event) { console.log('received message client!', event); }); client.on('disconnect', function () { clearinterval(interval); console.log('server has disconnected'); }); });
yes, design correct.
however, 1 thing should keep in mind take care of sending message correct client after authentication. in opinion, easy make mistake, partially because of simplicity of messaging using websockets.
Comments
Post a Comment