0

I Currently have a php website working. Here i can keep and use any session data to keep track of my user. however if i simply connect to my node.js game from this website using a simple hyper link such as...

<a href="http://localhost:8080">

This works and i do connect to my game running on a local host on that port here is the coded for setting up the node.js game.

const http = require('http');
const express = require('express');
const socketio = require('socket.io');

const TDSGame = require('./../tds-game');
const { log } = require('console');

const app = express();
// path for our client files

const clientPath = `${__dirname}/../client`;
console.log(`Serving static from ${clientPath}`);

// for static middleware from express
app.use(express.static(clientPath));

const server = http.createServer(app);
const io = socketio(server);

var waitingPlayers = [];

io.on('connection', (sock) => {

    if(waitingPlayers.length == 3){
       waitingPlayers.push(sock);
       new TDSGame(waitingPlayers);
       waitingPlayers = [];
    }
    else{
        waitingPlayers.push(sock);
        sock.emit('message', 'Waiting for opponent');
    }

    sock.on('message', (text)=>{
        // io.emmit everyone connected to the server receives the message
        io.emit('message', text);
    });
});


server.on('error', (err)=>{
    console.log('Server Error', err);
});


server.listen(8080, ()=>{
    console.log('TDS started on 8080');
});

What would be a good way of passing the players i dunno hash and username or something. to the the game so on connection i can get these variables and check to see if my player exists in the database? if so then pass these players and sockets to the game logic? I am struggling any help would be much appreciated thank you :)

CBroe
  • 91,630
  • 14
  • 92
  • 150
Ben
  • 45
  • 6
  • Does this answer your question? [PHP, nodeJS and sessions](https://stackoverflow.com/questions/24296212/php-nodejs-and-sessions) – reyad Jul 30 '20 at 11:10
  • for how to unserialize php sessions in nodejs, take a look [here](https://stackoverflow.com/questions/32544930/how-to-unserialize-php-session-in-node-js). – reyad Jul 30 '20 at 11:14

1 Answers1

0

you can add extra params to the socket connection URL "http://localhost:8080?foo=bar&hi=hello" and through that, you can get the data when the socket-clint connects ( io.on('connection') event ).

And you can delete the data from the array ( waitingPlayers ) when it disconnects. Through this way you can manage the connections.

I do use the socket.io for my chat-app where I use redis instead of Array to store the connection id to send and receive messages.

sachin
  • 1,075
  • 1
  • 7
  • 11