【问题标题】:SockJS-Nodejs server not listening & rendering html fileSockJS-Nodejs 服务器没有监听和渲染 html 文件
【发布时间】:2016-01-25 06:08:15
【问题描述】:

我正在生产环境中编写 Node-SockJS 服务器部署。编写服务器和客户端代码我面临的服务器没有呈现到所需的“index.html”文件。我在单独的文件夹中使用了编写客户端{index.js + index.html} 和服务器代码{sock_server.js}

我认为遇到的错误与文件处理或服务器未侦听正确端口有关。

sock_server.js

  //Required dependencies
    var http = require('http');
    var sockjs = require('sockjs');
    var connect = require('connect');
    var serveStatic = require('serve-static');


    //client list : this object will hold the list of all connected clients

    var clients = {};

     // Broadcast to all clients
    function broadcast(message){
      // iterate through each client in clients object
      for (var client in clients){
        // send the message to that client
        clients[client].write(JSON.stringify(message));
      }
    }

    // create sockjs server
    var echo = sockjs.createServer();

    // on new connection event
    echo.on('connection', function(conn) {

      // add this client to clients object
      clients[conn.id] = conn;

      // on receive new data from client event
      conn.on('data', function(message) {
        console.log(message);
        broadcast(JSON.parse(message));
      });

      // on connection close event
      conn.on('close', function() {
        delete clients[conn.id];
      });

    });

    // Create an http server
    var server = http.createServer();

    // Integrate SockJS and listen on /echo
    echo.installHandlers(server, {prefix:'/echo'});

    // Start server
    server.listen(9990, '0.0.0.0');
    // connect().use(serveStatic(__dirname)).listen(9990);
    console.log('Message listening on port 9990')

如果未注释,则倒数第二行会导致以下错误:

$ node sock_server.js
SockJS v0.3.15 bound to "/echo"
Message listening on port 9990
events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: listen EADDRINUSE
    at exports._errnoException (util.js:746:11)
    at Server._listen2 (net.js:1156:14)
    at listen (net.js:1182:10)
    at net.js:1280:9
    at dns.js:85:18
    at process._tickCallback (node.js:355:11)
    at Function.Module.runMain (module.js:503:11)
    at startup (node.js:129:16)
    at node.js:814:3

客户端代码

index.js

// First I need to Create a connection to http://localhost:9999/echo
var sock = new SockJS('http://localhost:9990/echo');
// var fs = require('fs');
// var index = fs.readFileSync('index.html');


// Open the connection
sock.onopen = function() {
  console.log('open');
};

// On connection close
sock.onclose = function() {
  console.log('close');
};

//show the message in text reciever area
// On receive message from server
sock.onmessage = function(e) {
  // Get the content
  var content = JSON.parse(e.data);

  // Append the text to text area (using jQuery)
  $('#chat-content').val(function(i, text){
    return text + 'User ' + content.username + ': ' + content.message + '\n';
  });

};

// Function for sending the message to server
function sendMessage(){
  // Get the content from the textbox
  var message = $('#message').val();
  var username = $('#username').val();

  // The object to send
  var send = {
    message: message,
    username: username
  };

  // Send it now
  sock.send(JSON.stringify(send));
}

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8"/>
    <title>Sock chat App</title>
  </head>
  <body>
    <textarea id="chat-content" style="width:500px;height:300px"></textarea><br/>
    <input type="text" id="username" placeholder="Choose username"/>
    <input type="text" id="message" placeholder="Enter chat message"/>
    <input type="button" value="Send" onclick="sendMessage()"/>

    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script>
    <script src="index.js" ></script>
  </body>
</html>

NodeJS 服务器正在启动并在 9990 端口运行。

$ netstat -atun

$ netstat -atun
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:5666            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:56133           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:9990            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp        0      0 172.31.41.71:22         182.75.84.170:60564     ESTABLISHED
tcp        0      0 172.31.41.71:22         182.75.84.170:59270     ESTABLISHED
tcp        0      0 127.0.0.1:32772         127.0.0.1:5672          ESTABLISHED
tcp        0     36 172.31.41.71:22         182.75.84.170:59272     ESTABLISHED
tcp        0      0 172.31.41.71:22         182.75.84.170:59271     ESTABLISHED
tcp        0      0 127.0.0.1:51461         127.0.0.1:4369          ESTABLISHED
tcp        0      0 172.31.41.71:40975      172.31.25.84:3306       ESTABLISHED
tcp        0      0 127.0.0.1:32771         127.0.0.1:5672          ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 :::5666                 :::*                    LISTEN
tcp6       0      0 :::5672                 :::*                    LISTEN
tcp6       0      0 :::6379                 :::*                    LISTEN
tcp6       0      0 :::111                  :::*                    LISTEN
tcp6       0      0 :::4369                 :::*                    LISTEN
tcp6       0      0 127.0.0.1:5672          127.0.0.1:32771         ESTABLISHED
tcp6       0      0 127.0.0.1:4369          127.0.0.1:51461         ESTABLISHED
tcp6       0      0 127.0.0.1:5672          127.0.0.1:32772         ESTABLISHED
udp        0      0 0.0.0.0:25052           0.0.0.0:*
udp        0      0 0.0.0.0:771             0.0.0.0:*
udp        0      0 0.0.0.0:68              0.0.0.0:*
udp        0      0 0.0.0.0:111             0.0.0.0:*
udp6       0      0 :::771                  :::*
udp6       0      0 :::18204                :::*
udp6       0      0 :::111                  :::* 

无法理解我做错了什么。

【问题讨论】:

    标签: javascript html node.js server sockjs


    【解决方案1】:

    拥有EADDRINUSE 看起来您之前的 Node 应用程序仍在端口 9990 上运行。

    运行

    ps aux | grep node
    

    然后

    kill -9 PID
    

    【讨论】:

    • 我猜,不是这样的,我已经停止{CTRL+C}“node sock_server.js”并在对文件进行更改后再次重新启动。它正在运行并且没有显示 EADDRINUSE 错误,我无法理解为什么 index.html 没有被打开/解析的基本问题
    • 我能期待更成熟的答案吗,我仍然不知道出了什么问题。
    猜你喜欢
    • 2019-12-24
    • 2015-06-06
    • 2020-06-27
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 2012-08-25
    • 2016-05-20
    • 1970-01-01
    相关资源
    最近更新 更多