Jump to content

Phaser + Websocket not working while browser is unfocussed


JokerJESUS
 Share

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:

Client:

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

var player;
var cursors;

function preload() {


	game.load.image('med1','client/assets/Unit/medievalUnit_01.png');
}

function create() {

	game.stage.disableVisibilityChange = true;

	game.physics.startSystem(Phaser.Physics.ARCADE);

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

	ws.send('NewPlayerJoined',nick);

	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');
		console.log("NewPpl");
	};

}


function update() {

	if (cursors.left.isDown) {

		player.body.velocity.x = -100;

	}else if (cursors.right.isDown) {

		player.body.velocity.x = 100;

	}else{

		player.body.velocity.x = 0;
	}


	if (cursors.up.isDown) {

		player.body.velocity.y = -100;

	}else if (cursors.down.isDown) {

		player.body.velocity.y = +100;

	}else{

		player.body.velocity.y = 0;
	}

}

 

SERVER:

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) {
        ws.send("sumf");
    });
});

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

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...