Friday 14 April 2017

Node.js, Socket.io emit not working

I am making a website in Socket.io. But emit method not working in my code. I can't see any errors in my code. Here is my server code.

var io = require("socket.io").listen(server);

//Some external codes for running server in Node.js

io.on("connection", function(socket) {
    //This line is working.
    console.log("Socket connected.");

    io.on("requestNewMap", function(data) {
        //Create new map object in here.
        io.emit("responseNewMap", { mapData: map });
    });
});

And this is my client side javascript code.

var socket = io();

socket.on("responseNewMap", function(data) {
    var map = data.mapData;

    //Draw map in canvas's context.
});

//setInterval runs this method every 2 seconds.
function requestNewMap() {
    socket.emit("requestNewMap");
}



via Stradivarius

No comments:

Post a Comment