Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 浏览器上的Node.js聊天客户端_Javascript_Jquery_Node.js_Sockets_Tcpsocket - Fatal编程技术网

Javascript 浏览器上的Node.js聊天客户端

Javascript 浏览器上的Node.js聊天客户端,javascript,jquery,node.js,sockets,tcpsocket,Javascript,Jquery,Node.js,Sockets,Tcpsocket,我有一个node.js聊天服务器,在那里我开发了一个iOS客户端,现在我想开发另一个可以在浏览器上使用的客户端。 我不想使用socket.io或类似的,只想使用纯tcp套接字。 这是服务器的代码: // Load the TCP Library net = require('net'); //var sys = require('sys'); // Keep track of the chat clients var clients = []; // Start a TCP Server n

我有一个node.js聊天服务器,在那里我开发了一个iOS客户端,现在我想开发另一个可以在浏览器上使用的客户端。 我不想使用socket.io或类似的,只想使用纯tcp套接字。 这是服务器的代码:

// Load the TCP Library
net = require('net');
//var sys = require('sys');

// Keep track of the chat clients
var clients = [];

// Start a TCP Server
net.createServer(function (socket) {

// Identify this client
socket.name = socket.remoteAddress + ":" + socket.remotePort 

// Put this new client in the list
clients.push(socket);

// Send a nice welcome message and announce
socket.write("Welcome " + socket.name + "\n");
broadcast(socket.name + " joined the chat\n", socket);

socket.write(tools.foo);

// Handle incoming messages from clients.
socket.on('data', function (data) {
broadcast(socket.name + " >> " + data+"\n", socket);
});

// Remove the client from the list when it leaves
socket.on('end', function () {
clients.splice(clients.indexOf(socket), 1);
broadcast(socket.name + " left the chat.\n");
});

// Send a message to all clients
function broadcast(message, sender) {
clients.forEach(function (client) {
  // Don't want to send it to sender
  if (client === sender) return;
  client.write(message);
});
// Log it to the server output too
process.stdout.write(message)
} 

}).listen(5100,"192.168.1.8");

// Put a friendly message on the terminal of the server.
console.log("Chat server running at port 5100\n");
我可以用什么来开发它?我希望使用javascript。
对不起,我是网络编程新手。

浏览器不能使用原始套接字。您需要使用WebSocket。如果您不想使用Socket.io,请随意重新发明轮子。这个问题与您以前的问题有关吗?你的前一张似乎比这张内容多一点。我马上开始工作!谢谢你的解释;)