如何从客户端通过node.js/socket.io连接到tcp服务器?

发布于 2024-12-03 12:02:00 字数 1200 浏览 2 评论 0原文

我已经使用node.js/socket.io 设置了一个tcp 服务器,但我不知道如何通过客户端连接到它。我已经尝试过 http://socket.io/#how-to-use 但它向 TCP 服务器发送 http 请求,建立连接后,网页未完成加载,加载圈继续移动。我可以从其他套接字看出,http 请求将所有标头发送到 tcp 服务器,但我不认为连接已建立,因为网页从未完全加载,并且我无法将其他任何内容传递到 tcp 服务器。如何将网页客户端建立到 TCP 服务器?

我的客户端:

<script type="text/javascript" src="http://localhost:82/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:81');

socket.on('connect', function () {
    socket.send('hi');
});

socket.send('hi1');
socket.emit('hi2');
</script>

我的服务器:

var app = require('net')
  , fs = require('fs')

var sockets_list = [];

var server = app.createServer(function (socket) {
  sockets_list.push(socket);
  socket.write("Echo server\r\n");

  socket.on('data', function(data) {
    console.log(data);
    for (var i = 0; i < sockets_list.length; i++) {
        sockets_list[i].write(data);
    }

  });

  socket.on('end', function() {
    var i = sockets_list.indexOf(socket);
    sockets_list.splice(i, 1);
  });

});

server.listen(81);

不用说,“hi”消息永远不会到达 TCP 服务器。

I've set up a tcp server using node.js/socket.io, but I can't figure out how to connect to it via the client side. I've tried the client code from http://socket.io/#how-to-use but it sends a http request to a tcp server and after the connection is made, the webpage does not finish loading the the loading circle continues to move. I can tell from other sockets that the http request sends all the headers to the tcp server, but I don't think the connection is established as the webpage never fully loads and I can't pass anything else to the tcp server. How do you establish the client side of the webpage to a tcp server?

My client:

<script type="text/javascript" src="http://localhost:82/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:81');

socket.on('connect', function () {
    socket.send('hi');
});

socket.send('hi1');
socket.emit('hi2');
</script>

My Server:

var app = require('net')
  , fs = require('fs')

var sockets_list = [];

var server = app.createServer(function (socket) {
  sockets_list.push(socket);
  socket.write("Echo server\r\n");

  socket.on('data', function(data) {
    console.log(data);
    for (var i = 0; i < sockets_list.length; i++) {
        sockets_list[i].write(data);
    }

  });

  socket.on('end', function() {
    var i = sockets_list.indexOf(socket);
    sockets_list.splice(i, 1);
  });

});

server.listen(81);

Needless to say, the 'hi' messages never reach the tcp server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掀纱窥君容 2024-12-10 12:02:00

我认为这是跨域请求问题。您从 localhost:82 加载页面,但向 localhost:81 发出请求。

I suppose this is Cross Domain Request problem. You load your page from localhost:82 but make request to localhost:81.

眉黛浅 2024-12-10 12:02:00

聚会有点晚了,也是我在 stackoverflow 上的第一个回答。

当我最初开始使用 socket.io 时,我遇到了同样的问题。问题在于服务器端代码,您不需要 socket.io 模块:

var io = require('socket.io');

io.on('connection',function(socket){
    console.log('socket connected successfully');

    socket.on('data',function(data){
        console.log('data',data);
    });
}

然后一旦服务器启动并运行,您就可以从中请求脚本“socket.io/socket.io.js”:

<script type="text/javascript" src="http://localhost:81/socket.io/socket.io.js">

我不'我不相信你可以将nodejs的核心“net”模块与socket.io的客户端库混合/匹配。

希望这有帮助。

A bit late to the party, and also my very first answer on stackoverflow.

I ran into the same problem when I initially started working with socket.io. The problem is with the server side code, you're not requiring the socket.io module:

var io = require('socket.io');

io.on('connection',function(socket){
    console.log('socket connected successfully');

    socket.on('data',function(data){
        console.log('data',data);
    });
}

Then once the server is up and running, you request the script 'socket.io/socket.io.js' from it:

<script type="text/javascript" src="http://localhost:81/socket.io/socket.io.js">

I don't believe you can mix/match the core 'net' module of nodejs with the client-side libraries of socket.io.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文