Nodo.js cliente para un socket.io servidor
Tengo un socket.io servidor en ejecución y una página web coincidente con una socket.io.js cliente. Todo funciona bien.
Pero, me pregunto si es posible, en otra máquina, ejecutar un nodo separado.js aplicación que actuaría como un cliente y conectarse a la mencionada socket.io ¿servidor?
110
Author: Andrew Myers, 2012-05-22
3 answers
Eso debería ser posible usando Socket. IO-client: https://github.com/LearnBoost/socket.io-client
70
Author: alessioalex,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-22 14:05:07
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-22 14:05:07
Añadiendo en ejemplo para la solución dada anteriormente. Usando socket.io-client
https://github.com/socketio/socket.io-client
Lado del cliente:
//client.js
var io = require('socket.io-client');
var socket = io.connect('http://localhost:3000', {reconnect: true});
// Add a connect listener
socket.on('connect', function (socket) {
console.log('Connected!');
});
socket.emit('CH01', 'me', 'test msg');
Lado del servidor:
//server.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.on('connection', function (socket){
console.log('connection');
socket.on('CH01', function (from, msg) {
console.log('MSG', from, ' saying ', msg);
});
});
http.listen(3000, function () {
console.log('listening on *:3000');
});
Ejecutar:
Abra la consola 2 y ejecute node server.js
y node client.js
29
Author: Azi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-17 11:22:41
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-17 11:22:41
Después de instalar socket. io-client:
npm install socket.io-client
Así es como se ve el código del cliente:
var io = require('socket.io-client'),
socket = io.connect('localhost', {
port: 1337
});
socket.on('connect', function () { console.log("socket connected"); });
socket.emit('private message', { user: 'me', msg: 'whazzzup?' });
Gracias alessioalex .
8
Author: Predrag Stojadinović,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:55:00
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:55:00