Web套接字服务器在带有节点命令的Ubuntu 12.04终端上不起作用(node server.js)

时间:2014-08-21 14:15:00

标签: node.js ubuntu websocket webserver webrtc

我想在ubuntu终端上运行带有node命令的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.");        
    });
});

但是当我进入节点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套接字服务器..

1 个答案:

答案 0 :(得分:0)

websocket是一个npm模块,您是否尝试使用以下方法将其安装到项目中:

npm install websocket

第一