[英]Node.js HTTP and TCP Clients Connection
I am trying to create a system where I have a desktop client created in VB, and a browser based client, that can send messages to each other. 我正在尝试创建一个系统,其中有一个在VB中创建的桌面客户端和一个基于浏览器的客户端,它们可以相互发送消息。 I am using a Node.js server to handle the connections and messages.
我正在使用Node.js服务器来处理连接和消息。
This is the code of my Node.js server: 这是我的Node.js服务器的代码:
net = require('net')
// Supports multiple client chat application
// Keep a pool of sockets ready for everyone
// Avoid dead sockets by responding to the 'end' event
var sockets = [];
// Create a TCP socket listener
var s = net.Server(function (socket) {
// Add the new client socket connection to the array of
// sockets
sockets.push(socket);
// 'data' is an event that means that a message was just sent by the
// client application
socket.on('data', function (msg_sent) {
// Loop through all of our sockets and send the data
for (var i = 0; i < sockets.length; i++) {
// Don't send the data back to the original sender
if (sockets[i] == socket) // don't send the message to yourself
continue;
// Write the msg sent by chat client
sockets[i].write(msg_sent);
}
});
// Use splice to get rid of the socket that is ending.
// The 'end' event means tcp client has disconnected.
socket.on('end', function () {
var i = sockets.indexOf(socket);
sockets.splice(i, 1);
});
});
s.listen(8000);
console.log('System waiting at http://localhost:8000');
With this sever, I am able to send messages between two desktop clients successfully. 使用此服务器,我能够在两个桌面客户端之间成功发送消息。 However, because I am using net and not HTTP I cannot get the browser based client to connect.
但是,因为我使用的是net而不是HTTP,所以无法连接基于浏览器的客户端。
How can I get both the clients to connect? 如何使两个客户端都可以连接? I would really appreciate any help/suggestions/directions.
我将不胜感激任何帮助/建议/方向。 I have been searching everywhere for about 4 days now!
我已经在各地搜索了大约4天! TIA!
TIA!
You could use http or express for browser based client. 您可以使用http或express表示基于浏览器的客户端。 could check socket.io also which works on http port.
可以检查也可以在http端口上使用的socket.io。
I would try to help more if know type of the desktop client you are using. 如果知道您使用的桌面客户端的类型,我将尝试提供更多帮助。
You need to use http or express for http client. 您需要对http客户端使用http或express。 For example :
例如 :
var express = require('express')
, app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server);
server.listen(8080);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.