javascript - Totaljs FrameworkWebSocket Client -


i use websockets send updates server clients.

i know can use server sent events internet explorer doesn't have great compatibility prefer use websocket.

the totaljs websocket allow me use client?

i'm trying this: (totaljs websocket example)

exports.install = function(framework) {     framework.route('/');     framework.route('/send/', send_message);      framework.websocket('/', socket_homepage, ['json']); };  function send_message() {     var controller = this;     var socket = new websocket('ws://127.0.0.1:8000/');     socket.onopen = function() {         socket.send(encodeuricomponent(json.stringify({ message: "send_message" })));     };     socket.close();     socket = null; }  function socket_homepage() {     var controller = this;      controller.on('open', function(client) {         console.log('connect / online:', controller.online);          client.send({ message: 'hello {0}'.format(client.id) });         controller.send({ message: 'connect new user: {0}\nonline: {1}'.format(client.id, controller.online) }, [], [client.id]);     });      controller.on('close', function(client) {         console.log('disconnect / online:', controller.online);         controller.send({ message: 'disconnect user: {0}\nonline: {1}'.format(client.id, controller.online) });     });      controller.on('message', function(client, message) {         console.log(message);          if (typeof(message.username) !== 'undefined') {             var old = client.id;             client.id = message.username;             controller.send({ message: 'rename: ' + old + ', new: ' + client.id });             return;         }          // send without client         message.message = client.id + ': ' + message.message;         console.log(message);         controller.send(message);     }); } 

when connect http://127.0.0.1/send clients connected server receive message.

node.js doesn't have native websocket support send_message() function not work. use totaljs websocket support don't know how use client.

thats it.

thank much.

i know not best way used global variable resolve.

exports.install = function(framework) {     framework.route('/');     framework.route('/send/', send_message);      framework.websocket('/', socket_homepage, ['json']); };  function send_message() {     if (websocket == null) return;     websocket.send({ message: "hello world!" }); }  var websocket = null; function socket_homepage() {     //var controller = this;     websocket = this;      websocket.on('open', function(client) {         console.log('connect / online:', websocket.online);          client.send({ message: 'hello {0}'.format(client.id) });         websocket.send({ message: 'connect new user: {0}\nonline: {1}'.format(client.id, websocket.online) }, [], [client.id]);     });      websocket.on('close', function(client) {         console.log('disconnect / online:', websocket.online);         websocket.send({ message: 'disconnect user: {0}\nonline: {1}'.format(client.id, websocket.online) });     });      websocket.on('message', function(client, message) {         console.log(message);          if (typeof(message.username) !== 'undefined') {             var old = client.id;             client.id = message.username;             websocket.send({ message: 'rename: ' + old + ', new: ' + client.id });             return;         }          // send without client         message.message = client.id + ': ' + message.message;         console.log(message);         websocket.send(message);     }); } 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -