连接到Node js TCP服务器后,客户端立即断开连接

时间:2013-04-06 14:50:57

标签: node.js sockets tcp websocket

这是我第一次使用节点js。我试图让客户端连接到服务器并保持连接而不关闭。

这些都托管在虚拟机托管的ubuntu服务器版本中。

我检查过服务器实际上正在使用端口5000监听 netstat -ant

我的浏览器也可以使用Websockets。

我得到以下输出错误

Echo server dot come
192.168.1.107
node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: EPIPE, Broken pipe
    at Socket._writeImpl (net.js:159:14)
    at Socket._writeOut (net.js:450:25)
    at Socket.write (net.js:377:17)
    at Socket.ondata (stream.js:36:26)
    at Socket.emit (events.js:81:20)
    at Socket._onReadable (net.js:678:14)
    at IOWatcher.onReadable [as callback] (net.js:177:10)

如果我在连接事件处理程序上的服务器上调用socket.end(),我会得到一个断开连接的客户端。我的问题是如何使用此代码建立稳定的连接,我需要改变什么,或者我在这里做错了什么?拉出我的头发 - 提前谢谢!

HTML CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test RS Server (built over nodejs)</title>
<link rel="stylesheet" href="style.css"></link> 
<script src="jquery.min.js" ></script>
<script src="client.js" ></script>
</head>
<body>
<label>Message</label>
<input type="text" id="message_box"></input>
<input type="button" id="submit_btn" value="Send"></input>
<div style="clear:both;"/>
<label>Output</label>
<textarea rows="10" cols="100" id="output_box"></textarea>

</body>
</html>

CLIENT JS

var message_box; 
var output_box;
var submit_btn;
var url = "wss://192.168.1.107:5000";
var socket;

function createListeners() {
    if ("WebSocket" in window) {
        output_box.html("Supports sockets.");
        socket= new WebSocket(url);
        socket.onopen= function() {
            socket.send("con opened");
            output_box.html("Connection opened.");
        };
        socket.onmessage= function(response) {
            //  alert('got reply '+s);
            console.log(response);
            output_box.html(response);
        };
        socket.onclose = function() { 
            // websocket is closed.
            output_box.html("Connection is closed..."); 
        };
        submit_btn.click(function(e) {
            socket.send(message_box.val());
        });
    } else {
        output_box.html("doesnt support sockets");
    };
};

$(document).ready(function() {
    message_box = $("#message_box");
    output_box = $("#output_box");
    submit_btn = $("#submit_btn");
    createListeners();
});

SERVER JS CODE

var net = require('net');
var server = net.createServer(function (socket) {
    socket.write('server says this\r\n');
    socket.pipe(socket);
});
server.on('connection', function(socket) {
    socket.write("Echo server dot come\r\n"+socket.address().address);
    console.log("Echo server dot come\r\n"+socket.address().address);
    // socket.end();
    socket.pipe(socket);
});
server.listen(5000);

1 个答案:

答案 0 :(得分:2)

您正在尝试将WebScoket客户端与简单的TCP服务器一起使用。这是一种不同的协议。您需要设置WebSocket服务器才能使其正常工作。您可以使用websocket模块来执行此操作。查看服务器示例代码here