我在生产环境中编写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 :::*
无法理解我做错了什么。
答案 0 :(得分:0)
让EADDRINUSE
看起来你以前的Node应用仍在端口9990上运行。
运行
ps aux | grep node
然后
kill -9 PID