I want to create a server listening for conections from standard websockets and socket.io websockets. Separately I can make them work just on the 3000 port but if both listen on that port then it just does not work, the standard websockets throw a lot of errors, so I was trying to make them listen on different ports, the socket.io (With an express server) on port 3000 and the standard websockets on 4000:
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const socketIO = require('socket.io');
const app = express()
const expresServer = http.createServer(app);
// Create socket.io instance and attach it to express
const io = socketIO(expresServer);
// Create websocket server on port 3000
const wss = new WebSocket.Server({ port: 4000 });
// WS connections:
wss.on('connection', (ws) => {
console.log('Nuevo cliente WebSocket conectado');
ws.on('message', (message) => {
console.log(`Recibido mensaje de WebSocket: ${message}`);
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(`Hola, recibí tu mensaje de WebSocket: ${message}`);
}
});
});
// Register diconect
ws.on('close', () => {
console.log('Cliente WebSocket desconectado');
});
});
// Socket.io connections:
io.on('connection', (socket) => {
console.log('Nuevo cliente Socket.io conectado');
socket.on('message', (message) => {
console.log(`Recibido mensaje de Socket.io: ${message}`);
io.emit('message', `Hola, recibí tu mensaje de Socket.io: ${message}`);
});
// Register diconect
socket.on('disconnect', () => {
console.log('Cliente Socket.io desconectado');
});
});
// Launch Express server on 3000
expresServer.listen(3000, () => {
console.log('Express server listening on 3000');
});
But when I try to connect with POCO C++ to the standard websockets it gives a Net Exception now. I just changed the port so I do not know why it does not work. I never opened the 3000 port so there is no reason to open 4000, is it?
This is the C++ client to connect with POCO C++:
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Net/HTTPMessage.h"
#include "Poco/Net/WebSocket.h"
#include "Poco/Net/HTTPClientSession.h"
#include <iostream>
using Poco::Net::HTTPClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPMessage;
using Poco::Net::WebSocket;
int main(int args,char **argv)
{
HTTPClientSession cs("192.168.50.122",4000);
HTTPRequest request(HTTPRequest::HTTP_GET, "/?encoding=text",HTTPMessage::HTTP_1_1);
request.set("origin", "Cliente");
HTTPResponse response;
try {
WebSocket* m_psock = new WebSocket(cs, request, response);
std::string text = "Hello, world!";
auto len = m_psock->sendFrame(text.c_str(), text.length(), WebSocket::FRAME_TEXT);
int flags=0;
char receiveBuff[256];
int rlen=m_psock->receiveFrame(receiveBuff,256,flags);
std::cout << receiveBuff << std::endl;
m_psock->close();
delete m_psock;
} catch (std::exception &e) {
std::cout << "Exception " << e.what();
}
}
This is how I made it work:
import { createServer } from "http";
import { Server } from "socket.io";
const server = createServer();
const server1 = createServer();
const io = new Server(server, {
// options
});
const io1 = new Server(server1, {
// options
});
const sockets = [io, io1];
sockets.forEach((socket) => {
socket.on("connection", (clientSocket) => {
// ...
});
});
server.listen(3000, () => console.log("listening on port 3000"));
server1.listen(4000, () => console.log("listening on port 4000"));
This was a try, so you can rename all variables for better programming experience.
For import to work, you need to add "type": "module",
to package.json, or convert to commonjs/require.