Web套接字服务器不能在Ubuntu 12.04终端上使用Node命令(Node Server .js)



我想在ubuntu终端上运行带有node命令的web socket服务器。我的web套接字服务器在下面->

var WebSocketServer = require('websocket').server;
//var WebSocketServer = require('websocket');
var http = require('http');
var clients = [];
var server = http.createServer(function(request, response) {
    // process HTTP request. Since we're writing just WebSockets server
    // we don't have to implement anything.
});
server.listen(1337, function() {
  console.log((new Date()) + " Server is listening on port 1337");
});
// create the server
wsServer = new WebSocketServer({
    httpServer: server
});
function sendCallback(err) {
    if (err) console.error("send() error: " + err);
}
// This callback function is called every time someone
// tries to connect to the WebSocket server
wsServer.on('request', function(request) {
    console.log((new Date()) + ' Connection from origin ' + request.origin + '.');
    var connection = request.accept(null, request.origin);
    console.log(' Connection ' + connection.remoteAddress);
    clients.push(connection);
    // This is the most important callback for us, we'll handle
    // all messages from users here.
    connection.on('message', function(message) {
        if (message.type === 'utf8') {
            // process WebSocket message
            console.log((new Date()) + ' Received Message ' + message.utf8Data);
            // broadcast message to all connected clients
            clients.forEach(function (outputConnection) {
                if (outputConnection != connection) {
                  outputConnection.send(message.utf8Data, sendCallback);
                }
            });
        }
    });
    connection.on('close', function(connection) {
        // close user connection
        console.log((new Date()) + " Peer disconnected.");        
    });
});

但是当我输入'node server.js'命令在1337端口上运行服务器时,我得到了如下错误->

module.js:340
    throw err;
          ^
Error: Cannot find module 'websocket'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/var/www/example_directory/server.js:1:85)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

我安装了node.js的所有包,但是node命令不能在终端上使用我的web套接字服务器.

websocket是一个NPM模块,你是否尝试使用以下方式将它安装到你的项目中:

npm install websocket

吗?

相关内容

  • 没有找到相关文章

最新更新