I donβt know if this will help, but you can make an abstraction on the client based on your browser, and then create a separate get function on the server that will process the request in the same way as socket.on Call me back. To find out where to send the information, I suggest that you use some key that can be stored in a hash table on the server and local storage on the client.
For client:
var emit = function(event, options) { if ("WebSocket" in window) { socket.emit(event, options); console.log("emited via WebSocket"); } else { $.post("http://localhost/emit/" + event, options); console.log("emited via AJAX"); } } emit("echo", { key: localStorage.getItem("key"), data: { hello: "world" } }); socket.on("response", function(data) { console.log(data.hello);
For server:
var sockets = {}; var echo_handler = function(a) { var socket = sockets[a.key]; var data = a.data; socket.emit("response", data); } app.post("/emit/:event", function(req, res) { var event = req.params.event; switch (event) { case "echo": var a = { key: req.param("key"), data: req.param("data") } echo_handler(a); break; } }); io.sockets.on("connection", function(socket) { socket.on("connect", function(data) { sockets[data.key] = socket; }); socket.on("echo", echo_handler); });
Another way to do this would be to switch to Sockjs and use the patch.
If someone has a better solution for Socket.IO, it will be appreciated because I'm already deep in the project, and it's too late to switch Socket.IO for Sockjs, and I don't like this solution :(.
source share