Sign in to follow this  

Phaser + Websocket not working while browser is unfocussed

Recommended Posts

Hello :)

I started playing with phaser, some time ago. 

But now i wanted to try something new, something like Websocket + phaser. And i stuck :/

So, i want to make multiplayer "Game" but i stuck, i cannot happen that game will be still working in background.

It should be working that i m logging at 1st tab and sprite is apearing, than i m logging at 2nd tab and on first another sprite is apearing but on different position.

So at the end, at the 1st tab will be 3 sprites, or more, depends how many tabs you will open.

Sry for my bad engladno :/ I hope you understood something.


My code:


var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });

var player;
var cursors;

function preload() {


function create() {

	game.stage.disableVisibilityChange = true;


	cursors = game.input.keyboard.createCursorKeys();
	game.input.mouse.capture = true;


	player = game.add.sprite(0,0,'med1');
	player.nickname = nick;

	game.physics.enable(player, Phaser.Physics.ARCADE);

	ws.onmessage = function(data){
		game.add.sprite(Math.floor((Math.random() * 100) + 1),Math.floor((Math.random() * 200) + 1),'med1');


function update() {

	if (cursors.left.isDown) {

		player.body.velocity.x = -100;

	}else if (cursors.right.isDown) {

		player.body.velocity.x = 100;


		player.body.velocity.x = 0;

	if (cursors.up.isDown) {

		player.body.velocity.y = -100;

	}else if (cursors.down.isDown) {

		player.body.velocity.y = +100;


		player.body.velocity.y = 0;




const express = require('express');
const http = require('http');
const url = require('url');
const WebSocket = require('ws');

const app = express();

var players = {};

__dirname = 'E:/WebstormProjects/TestWorkingServer';

app.get('/',function(req, res) {
    res.sendFile(__dirname + '/client/index.html');
app.use('/client',express.static(__dirname + '/client'));

const server = http.createServer(app);
const wss = new WebSocket.Server({ server });

wss.on('connection', function connection(ws) {
console.log("New Connection");

    ws.on("message",function incoming(data) {

server.listen(2000, function listening() {
    console.log('Listening on %d', server.address().port);


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.