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 :)

2
  • Does this answer your question? PHP, nodeJS and sessions Commented Jul 30, 2020 at 11:10
  • for how to unserialize php sessions in nodejs, take a look here. Commented Jul 30, 2020 at 11:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.