简体   繁体   English

从 PHP 网站 node.js 多人游戏连接。 如何跟踪用户?

[英]connecting from PHP website node.js multiplayer game. How to track user?

I Currently have a php website working.我目前有一个 php 网站正在运行。 Here i can keep and use any session data to keep track of my user.在这里,我可以保留和使用任何 session 数据来跟踪我的用户。 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.这可行,我确实连接到我在该端口上的本地主机上运行的游戏,这里是设置 node.js 游戏的代码。

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.什么是传递球员的好方法我不知道 hash 和用户名什么的。 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?如果是这样,那么将这些玩家和 sockets 传递给游戏逻辑? I am struggling any help would be much appreciated thank you:)我正在努力任何帮助将不胜感激谢谢:)

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 ).您可以向套接字连接 URL "http://localhost:8080?foo=bar&hi=hello" 添加额外的参数,通过它,您可以在套接字连接时获取数据( io.on('connection')事件)。

And you can delete the data from the array ( waitingPlayers ) when it disconnects.并且您可以在断开连接时从数组( waitingPlayers )中删除数据。 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.我确实将 socket.io 用于我的聊天应用程序,其中我使用 redis 而不是 Array 来存储连接 ID 以发送和接收消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM