display mouse pointer movement in other client computers using socket.io - node.js

I'm using socket.io engine to track the mouse pointer movement from the browser and send it to multiple web browsers. Here I get the x, y coordinates of the mouse pointer and then send it using socket.io engine.
socket.emit('mouse_position', {mx : x, my : y});
after that I get the relevant data from the application by following code
socket.on('mouse_position', function(data) {
socket.broadcast.emit('mouse_position_update', data);
});
the I use a raphael object to show the mouse pointer in other browsers and use animate function to display the mouse pointer according to the parent mouse pointer movement.
var paper = new Raphael(canvas, 200, 200);
var cur = paper.circle(0, 0, 3);
cur.animate({
cx : data.mx,
cy : data.my
}, 1, 'linear');
the problem in this code is that if I lot of users (>100) log into this system , lot of bandwidth will be used and system may get crashed. can anybody tell me is there a better way to implement this system using socket.io engine or is there any algorithm we can use to solve the bandwidth issue.
Any kind of help would be a great help for me .

You should decrease the amount of time you send you mouse coordinates. In your way every pixel your mouse is changed, your data is sent. (put console.log(1) in your callback and move your mouse for a few seconds).
You can decrease the amount of time you send you data in the following way:
var prevMouseX, prevMouseY;
var intervalID = window.setInterval(function(){
... you get your mouse coordinates
if (prevMouseX !== x || !prevMouseY !== y) {
socket.emit('mouse_position', {mx : x, my : y});
}
}, 500);
This way you will send your mouse coordinates every 0.5 seconds and only if they changed.

Related

NodeJS callbacks and forloops

So I'm working on a text-game-thingy when I realize that I'm getting an error every once-in-a-while (~1/100 refreshes) when leaving the game. I have a basic object holding properties containing a player object. I also have a 'textCooldown' property in my player object (to stop you from spamming), and a 'setInterval' function to reduce this property's value every x seconds that looks, for all intensive purposes, like this:
setInterval(function() {
for(var p in PLAYER_LIST) {
var player = PLAYER_LIST[p];
player.textCooldown -= 0.1;
}
}, 100);
I also have a function that gets called whenever the client disconnects, which deletes that property inside PLAYER_LIST. The problem is, every once in a while when a client disconnects the server is still calling the 'for' loop (which tells me NodeJS is multithreaded in at least some aspects) and tries to change the property 'textCooldown' of an undefined object.
How can I fix this problem without putting if(typeof player != 'undefined' everywhere? Is there any way I can implement callbacks into this seemingly synchronous task?

My GeoLocation Does not Seem to use Cache

I see alot of people complaining about how GeoLocation uses cache.... But my problem is that mine does not seem to use cache. My Weather API needs coordinates in order to be called, and the function to call the API is triggered once my GeoLocation gets its coordinates. The results appear on an HTML page, and if I open up a bunch quickly its almost instant, but after maybe 10 seconds it stops using the cache. I set maximumAge to 5400000, 1.5 hours, and that does not seem to work. Please let me know if you have any ideas! Thanks so much in advance.
Here is my GeoLocation Code if you are interested:
navigator.geolocation.getCurrentPosition(successFunction, errorFunction, geo_options);
function successFunction(position) {
lat = position.coords.latitude;
long = position.coords.longitude;
console.log("lat:" + lat + " long:" + long);
callWeather(); // function that contains API call and Jquery to append
//the HTML file
}
function errorFunction(position) {
console.log('Error with Position!');
}
var geo_options = {
maximumAge: 54000000, //wait an hour and a half before updating position,
//allows for quicker calls to weather api?
timeout: 10000 // wait 10 seconds before giving up on position
};
I added this line to find out where the lag really was-
console.log("called position:"+d.getSeconds()+":"+ d.getMilliseconds());
and found out it was my weather api that was holding back the HTML display:
from the log (it is in minutes/seconds/milliseconds):
content.js:232 called position: 48:36:915
content.js:329 called weather: 48:39:150
I'm using DarkSky for my weather API. I wonder how I can speed that up...

Sprite movement stutters a lot randomly

I have a sprite and I set its y velocity to 200 so that it moves down.
The sprite moves perfectly fine except that sometimes it stutters a lot. The other times it is silky smooth. Its like the fps drops to 20.
How do I stop this stuttering?
Below is my code and you can try it live here
var SimpleGame = (function () {
function SimpleGame() {
this.game = new Phaser.Game(800, 400, Phaser.AUTO, 'content', { preload: this.preload, create: this.create, update: this.update });
}
SimpleGame.prototype.preload = function () {
this.game.load.image('logo', 'Sprites/icon.png');
};
SimpleGame.prototype.create = function () {
//Create Sprite
this.speed = 133;
this.game.stage.backgroundColor = 0xffffff;
this.logo = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'logo');
this.logo.position.set(200, 50);
this.game.physics.arcade.enable(this.logo);
//Set velocity
this.logo.body.velocity.y = this.speed;
};
return SimpleGame;
})();
window.onload = function () {
var game = new SimpleGame();
};
I am not getting the stuttering myself. So, try loading it with other computers, find friends with different levels of computers, to make sure that it isn't a local client-side problem. If it is, then check your computers firewall settings any antivirus that may be stopping a process which will slow down your game.
If it is a server-side problem- firstly, try to condense your code and the memory used. Use local variables instead of global ones, don't pass too many function arguments (If you are using a lot of function arguments then you are filling up the Stack and that may be causing the lag).
Also check your server. I don't know what web server you are using so I don't really know your specs on this, but I may be able to help. Does Phaser use too much memory for your webserver? Small Webservers are designed purely for small websites so by using a lot of JS (which Phaser does ,look in Phaser.min!) you may be using too much memory on your server. Maybe a bigger subscription?
I hope I've helped.

Flooding Socket.IO Node server

I am doing an interactive game with a Raspberry Pi and Sockets and I emit with a high rate (1-20 per second per listener) from the RPI to the server that's on the laptop.
At some times everything is ok but other times it gets really buggy (taking a few ms to display changes).
Is there a way to optimize this communication with Socket.IO or are there any alternative ways on doing this?
P.S. I'm doing plain emits with no data to be lighter.
EDIT:
Server:
socket.on('player1', function(){
//doing calculations
});
socket.on('player2', function(){
//doing calculations
});
socket.on('player3', function(){
//doing calculations
});
Client:
socket.emit('player1');
socket.emit('player2');
socket.emit('player3');
EDIT 2
These are the "calculations" that I am doing when I receive an emit from the Pi, it's some very basic RaphaelJS object animations.
var pos = this.objects[index].getPointAtLength(this.counter[index]);
this.e[index].attr({
x: pos.x,
y: pos.y
});
this.counter[index]++;

spotify session callback get_audio_buffer_stats

I'm trying to make a program in Spotify that collects the audio data. I saw in the API that there is a callback get_audio_buffer_stats, which has stutter and samples. I tried adding that to the program (I am just modifying the jukebox example), but it only ever prints 0 for stutter and samples, even when I turn off the wifi and wait for the song to stop playing. And by adding the code, I mean that I made a callback function for it, and I added it to the session callbacks. Am I missing something? Can anyone help me to get this callback to work? Thanks! My code is below:
static void get_audio_buffer_stats(sp_session *sess, sp_audio_buffer_stats *stats)
{
pthread_mutex_lock(&g_notify_mutex);
//log session data
stuttervariable = stats->stutter;
samplesvariable = stats->samples;
printf("stutter, %d\n", stuttervariable);
printf("samples, %d\n", samplesvariable);
pthread_cond_signal(&g_notify_cond);
pthread_mutex_unlock(&g_notify_mutex);
}
/**
* The session callbacks
*/
static sp_session_callbacks session_callbacks = {
.logged_in = &logged_in,
.notify_main_thread = &notify_main_thread,
.music_delivery = &music_delivery,
.metadata_updated = &metadata_updated,
.play_token_lost = &play_token_lost,
.log_message = NULL,
.end_of_track = &end_of_track,
.get_audio_buffer_stats = &get_audio_buffer_stats,
};
I think the idea with get_audio_buffer_stats is that you are supposed to tell libspotify if you've suffered stuttering and how many samples are left in your buffer. When it calls get_audio_buffer_stats, it passes a pointer to a struct that you are supposed to fill in. Presumably if you tell libspotify that you're suffering stutter it will try to send you a bit more data to keep your buffer more full. By telling libspotify how full your buffer is, it can accommodate for drift in your clock causing you to consume audio slightly faster or slower than it expects.

Resources