I just need a simple example of how to make a Node.JS server, which I will explain.
Basically, Node.JS will operate 2 servers: - An uncleaned TCP server and - A Socket.IO server. The goal is to transfer data from a TCP client to various interested Socket.IO clients.
The reason for this is to simplify communication with other languages ββ(I will have a Java server sending messages to the tcp socket, since I could not find a better way to do this - all available java libraries (socket.io server and clients implemented in java) are errors ), since almost every language has an api socket.
The TCP client will send the string immediately after connecting, and the Node.JS server will create a namespace with it and provide data for it, so that Socket.IO clients should be able to receive data that the server will forward from the TCP client.
Steps:
Listen to TCP connections
In a new TCP connection, get a string from the client
Create a Socket.IO server with a namespace with the name provided from the tcp client
Start receiving data from the TCP client and transfer it to all Socket.IO clients associated with the namespace that was created when this socket was opened
This should be done so that various TCP clients and Socket.IO clients can communicate
I cannot find how to do this in an efficient way, if someone can provide a simple example, I am sure that this will help a lot of ppl (since Node.JS and Socket.IO lack documentation) and this is easy to do for ppl. who already know about it.
Thanks.
UPDATE:
I have done this:
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 + '\r\n'); } 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 does not connect through nodejs via a TCP socket and then sends the namespace name, nodejs creates a socket.io server with that namespace and forwards all the messages. java socket sends to all socket.io clients associated with this namespace
If anyone sees bugs or improvements that can be made, split up.
thanks