Socket.io有2台本地机器?

时间:2015-12-31 17:42:54

标签: node.js express socket.io

我试图使用socket.io从我的快速服务器发送raspberry pi事件。我似乎无法让我的覆盆子pi连接到服务器。关于我做错了什么的任何想法?

Rasberry Pi app:

  var socket = require('socket.io-client')('192.168.0.10:3000');
  console.log('running');
  socket.on('connect', function(){
    console.log('connected');
  });
  socket.on('event', function(data){
    console.log(data);
  });
  socket.on('disconnect', function(){
    console.log('disconnected');
  });

Express Server:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendfile('index.html');
});

io.emit('event', 'hello');

io.on('connection', function(socket){
  console.log('a user connected');
});
io.on('disconnect', function(socket){
  console.log('a user disconnected');
});
http.listen(3000, function(){
  console.log('listening on *:3000');
});

1 个答案:

答案 0 :(得分:0)

需要更改我的Raspberry Pi的第一行以包含 http://

来自:

var socket = require('socket.io-client')('192.168.0.10:3000');

var socket = require('socket.io-client')('http://192.168.0.10:3000');