Javascript Node.js and Socket.IO broadcast server
I just need a simple example how to do a Node.JS server that I'll explain.
Basically Node.JS will have 2 servers running: - A crude TCP server and - A Socket.IO server The objective is to forward data from a TCP Client to various Socket.IO clients interested in it
The reason to do this is to make easy to do the communication with other languages (I'll have a java server sending messages in a tcp socket, as I couldnt find a better way to do this - all available java libraries (socket.io server and clients implemented in java) are buggy) as almost every language has socket api.
The TCP client will send a string right after connect and the Node.JS server will create a namespace with it and provide data for it so Socket.IO clients must be able to receive the data that the server will forward from the TCP client.
Steps:
Listen for TCP connections
On a new TCP connection receive a string from the client
Create a Socket.IO server with a namespace named the string provided from tcp client
Start receiving data from TCP client and broadcast it to all the Socket.IO clients conected to the namespace that was created when this socket opened
This has to be made in a way that various TCP clients and Socket.IO clients can communicate
I cant find how to do this in a efficient way, if someone can provide a simple example I'm sure it would help a lot of ppl (as Node.JS and Socket.IO lacks documentation) and this is easy to make for ppl that already know of the subject.
Thanks.
UPDATE:
I made it:
node.js code:
var javaPort = 8080;
var sIOPort = 8081;
var javaServer = require('net').createServer();
var browserServer = require('socket.io').listen(sIOPort);
console.log('Socket.IO version: ' + require('socket.io').version);
javaServer.on('listening', function () {
console.log('Server is listening on ' + javaPort);
});
javaServer.on('error', function (e) {
console.log('Server error: ' + e.code);
});
javaServer.on('close', function () {
console.log('Server closed');
});
javaServer.on('connection', function (javaSocket) {
var clientAddress = javaSocket.address().address + ':' + javaSocket.address().port;
console.log('Java ' + clientAddress + ' connected');
var firstDataListenner = function (data) {
console.log('Received namespace from java: ' + data);
javaSocket.removeListener('data', firstDataListenner);
createNamespace(data, javaSocket);
}
javaSocket.on('data', firstDataListenner);
javaSocket.on('close', function() {
console.log('Java ' + clientAddress + ' disconnected');
});
});
javaServer.listen(javaPort);
function createNamespace(namespaceName, javaSocket) {
var browserConnectionListenner = function (browserSocket) {
console.log('Browser Connected');
var javaSocketDataListenner = function(data) {
console.log('Data received from java socket and sent to browser: ' + data);
browserSocket.emit('m', data + 'rn');
}
var javaSocketClosedListenner = function() {
console.log('The java socket that was providing data has been closed, removing namespace');
browserSocket.disconnect();
browserServer.of('/' + namespaceName).removeListener('connection', browserConnectionListenner);
javaSocket.removeListener('data', javaSocketDataListenner);
javaSocket.removeListener('close', javaSocketClosedListenner);
}
javaSocket.on('close', javaSocketClosedListenner);
javaSocket.on('data', javaSocketDataListenner);
browserSocket.on('disconnect', function () {
console.log('Browser Disconnected');
javaSocket.removeListener('data', javaSocketDataListenner);
javaSocket.removeListener('close', javaSocketClosedListenner);
});
}
var namespace = browserServer.of('/' + namespaceName).on('connection', browserConnectionListenner);
}
java code:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
Socket nodejs = new Socket("localhost", 8080);
sendMessage(nodejs, "testnamespace");
Thread.sleep(100);
int x = 0;
while (true)
{
sendMessage(nodejs, x + "");
x++;
Thread.sleep(1000);
System.out.println(x + " has been sent to server");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void sendMessage(Socket s, String message) throws IOException {
s.getOutputStream().write(message.getBytes("UTF-8"));
s.getOutputStream().flush();
}
public static String readMessage(Socket s) throws IOException {
InputStream is = s.getInputStream();
int curr = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((curr = is.read()) != -1) {
if (curr == 'n') {
break;
}
baos.write(curr);
}
return baos.toString("UTF-8");
}
}
html code:
<html>
<head>
<script src="socket.io.js"></script>
</head>
<body>
<script>
var socket = io.connect("http://localhost/testnamespace", {port: 8081});
console.log(io.version);
socket.on('connect', function () {
console.log('Connected');
});
socket.on('m', function (msg) {
console.log('Message received: ' + msg);
});
socket.on('disconnect', function () {
console.log('Disconnected');
});
</script>
</body>
</html>
How it works:
Java connect no nodejs over TCP socket then send a namespace name, nodejs create a socket.io server with that namespace and forward all messages java socket sends to all socket.io clients connected to that namespace
If anyone see errors or improvements that could be made please share.
Thanks
I'm making a similar app, but instead of using TCP I'm using ZeroMQ on top of TCP. This code works very simply.
Check out my code here: https://github.com/EhevuTov/netPeek
Do:
$:node support/msu_gen.js
$:node app.js
I hope that helps.
As a side note, I don't think my callback function is properly implemented. I think I'm setting the on event object every time a message gets passed. If you find a better way, please submit pull request with a fix. :-)
链接地址: http://www.djcxy.com/p/94588.html