客户端 tcp 与 node.js/socket.io? [英] Client side tcp with node.js/socket.io?

查看:35
本文介绍了客户端 tcp 与 node.js/socket.io?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 node.js/socket.io 的新手,所以我真的不知道我在做什么,但我的服务器端有这个:

I'm new to node.js/socket.io so I don't really know what I'm doing, but I've got this for my server side:

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


var server = app.createServer(function (socket) {
  socket.write("Echo server\r\n");
  socket.pipe(socket);
  socket.on('data', function(data) {
    console.log(data);
  });
});

var io = require('socket.io').listen(server)

server.listen(81);

因此,当我使用 nc localhost 81 并向其发送数据时,tcp 服务器可以工作.但是,我不知道如何使用脚本标签从服务器端连接和发送数据到网站上的 tcp 服务器.那么客户端连接到这个tcp服务器并向它发送数据会是什么?

So the tcp server works when I use nc localhost 81 and send it data. However, I don't know how to connect and send data to the tcp server on a website from the server side using script tags. So what would the client side be to connect to this tcp server and send data to it?

谢谢!

新增代码:

服务器:

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);

客户:

<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');

alert('here');



</script>

发生了什么:

我有一个网页加载,它占用一个套接字和一个占用另一个套接字的 nc localhost 81.因此 nc 显示网页发送的所有内容.连接网页后, alert('here');执行并且nc显示http请求,但是,没有其他任何内容发送到tcp服务器,并且网页处于不断刷新状态.为什么网页永远不会完全加载,永远不会发送hi"消息,以及对 tcp 服务器的 http 请求呢?为什么我的 tcp 客户端在我的 tcp 服务器上没有失败?

I have a webpage loading which takes one socket and a nc localhost 81 which takes up another socket. So the nc displays everything the web page sends. After I connect the webpage, the alert('here'); is executed and the nc shows the http request, however, nothing else is sent to the tcp server, and the webpage is in constant refresh status. Why does the webpage never fully load, the 'hi' messages never get sent, and what about the http request to a tcp server? Why doesn't my tcp client fail with my tcp server?

推荐答案

Socket.io的如何使用 页面向您展示了如何做到这一点.

Socket.io's how to use page shows you exactly how to do that.

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost/');
  socket.on('connect', function () {
    socket.send('hi');

    socket.on('message', function (msg) {
      // my msg
    });
  });
</script>

这篇关于客户端 tcp 与 node.js/socket.io?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆