我正在尝试从另一台连接到同一wifi的计算机连接到服务器。如果我打开多个客户端浏览器,则此代码有效,但当我要将另一台计算机连接至同一服务器时,此代码无效。服务器是否有可能使用此代码从另一台计算机连接到它?
"use strict";
process.title = 'node-chat';
// Port where we'll run the websocket server
var webSocketsServerPort = 1337;
// websocket and http servers
var webSocketServer = require('websocket').server;
var http = require('http');
/**
* Global variables
*/
// list of currently connected clients (users)
var clients = [ ];
/**
* HTTP server
*/
var server = http.createServer(function(request, response) {
});
server.listen(webSocketsServerPort, function() {
console.log((new Date()) + " Server is listening on port " + webSocketsServerPort);
});
/**
* WebSocket server
*/
var wsServer = new webSocketServer({
httpServer: server
});
wsServer.on('request', function(request) {
console.log('workssssss');
console.log((new Date()) + ' Connection from origin ' + request.origin + '.');
var connection = request.accept(null, request.origin);
//clients.push(connection);
// we need to know client index to remove them on 'close' event
var index = clients.push(connection) - 1;
console.log("USER ID: ", index);
// user sent some message
connection.on('message', function(message) {
//broadcast the message to all the clients
clients.forEach(function(client) {
console.log(message);
client.send(message.utf8Data);
});
});
});