我用生成器Angular-Fullstack开始了一个项目。我希望外部节点客户端(在其他机器上运行)可以连接到Angular-FullStack项目中的套接字服务器。
我用简单的节点服务器和客户端测试了它,它工作正常。代码是:
NODE SERVER:
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var port = 9000;
server.listen(port, function(){
console.log('Express server listening on %d port', port);
});
// __dirname is the directory where this file is placed
app.get('/', function (req, res) {
res.sendFile(__dirname + '/browserSocketAppClient.html');
});
io.on('connection', function (socket) {
console.log( 'THE SOCKET: ' + socket.toString());
socket.emit('NODE SOCKET SERVER', { SERVER: 'NODE SOCKET SERVER' });
socket.on('NODE SOCKET CLIENT', function (data) {
console.log(data);
});
});
NODE客户:
var io = require('socket.io-client');
var socket = io.connect('http://192.168.0.101:9000');
socket.on('connect', function() {
console.log( 'NODE SOCKET CLIENT has connected with the server');
});
socket.on('NODE SOCKET SERVER', function (data) {
console.log(data);
socket.emit('NODE SOCKET CLIENT', { CLIENT: 'NODE SOCKET CLIENT' });
});
服务器控制台:
D:\003_IOT\CODE_EXAMPLES\TestSocketIO>node nodeSocketServer
Express server listening on 9000 port
{ CLIENT: 'NODE SOCKET CLIENT' }
客户控制台
D:\003_IOT\CODE_EXAMPLES\TestSocketIO>node nodeSocketClient
NODE SOCKET CLIENT has connected with the server
{ SERVER: 'NODE SOCKET SERVER' }
但是当我想使用相同的NODE客户端时:使用Angular-Fullstack项目,它不起作用。
我已将此代码添加到myproject / server / config / socketio.js
socketio.js
function onConnect(socket) {
socket.emit('NODE SOCKET SERVER', { SERVER: 'You are connected with NODE SOCKET SERVER' });
socket.on('NODE SOCKET CLIENT', function (data) {
console.log(data);
});
............................
}
我进入服务器的控制台这个递归消息,
Express server listening on NaN : 9000 in development mode
finished populating users
GET /socket.io/?EIO=3&transport=polling&t=1438187488692-7 200 27ms
[undefined:undefined] SOCKET CONNECTED
GET /socket.io/?EIO=3&transport=polling&t=1438187489437-306 200 9ms
GET /socket.io/?EIO=3&transport=polling&t=1438187493792-8 200 8ms
GET /socket.io/?EIO=3&transport=polling&t=1438187495438-307 200 5ms
GET /socket.io/?EIO=3&transport=polling&t=1438187498815-9 200 4ms
GET /socket.io/?EIO=3&transport=polling&t=1438187501438-308 200 5ms
GET /socket.io/?EIO=3&transport=polling&t=1438187503827-10 200 7ms
但它从未真正联系过。 它必须是Angular-Fullstack项目服务器端配置中的东西,但我不能用它来填充它。
任何想法谢谢