I'm trying to put together a simple Socket.IO test case in Node.js, but I'm not seeing the events emitted from the client getting to the server.
var http = require("http"),
io = require("socket.io"),
io_client = require("socket.io-client"),
listener, port, server, url,
socketServer, socketClient,
i = 0;
port = process.env.LISTEN_PORT || 80;
url = "http://localhost:" + port;
server = http.createServer();
socketServer = io(server);
socketClient = io_client(url);
socketServer.on("connection", function(socket) {
console.log("server:connection");
socket.emit("ping", ++i);
socket.on("pong", function(data) {
console.log("server:pong", data);
});
});
socketClient.on("ping", function(data) {
console.log("client:ping", data);
socketClient.emit("pong", data);
});
listener = server.listen(port, function() {
console.log("Listening on port " + listener.address().port);
});
expected output
Listening on port 80
server:connection
client:ping 1
server:pong 1
actual output
Listening on port 80
server:connection
client:ping 1
The connection is made, data is moving from server to client, but I don't see the corresponding events emitted on the server when the client triggers a socket event.
Copyright Notice:Content Author:「rich remer」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/36229998/node-js-socket-io-client-emit-not-seen-on-server