每个用户单独的空间

时间:2019-09-21 13:32:25

标签: c# node.js unity3d socket.io

对不起,我英语不好。该应用程序从智能手机的加速度计读取数据。他有一台服务器,当连接智能手机时,会在其中创建一对有关空间位置的id数据。目前,所有用户的数据都到达服务器。任务是相反的:每次用户连接时,都会创建一个单独的空间,仅从一个ID接收数据。

服务器代码:

import {NavLink} from 'react-router-dom';

<BrowserRouter>
       //exact
       <Route path='' exact component={}/>
       <Route path='' exact component={}/>
</BrowserRouter>


<NavLink to={} exact />

套接字代码:


    var express = require('express');
    var compression = require('compression');
    var http = require('http');
    var path = require('path');
    var socketIO = require('socket.io');
    const port = process.env.PORT || 5000;
    var cors = require('cors');




    var app = express();
    var server = http.Server(app);
    var io = socketIO(server);
    app.use(cors()) ;// Use this after the variable declaration

    app.set('port', port);
    app.use(compression());
    app.use(express.static(__dirname + '/'));
    app.use('/static', express.static(__dirname + '/static'));
    // Routing
    app.get('/', function (request, response) {
        response.sendFile(path.join(__dirname, 'view.html'));
        console.log(Object.keys(player_move).length);
        if (Object.keys(player_move).length >= 1){
            let data = {
                id: socketId,
                x: player_move[socketId].x,
                z: player_move[socketId].z,
            };
            response.send(data);
            console.log('connected');
        }else{
            response.send("no");
            console.log('not connected');
        };

    });
    app.use(express.static(__dirname + '/client/'));
    // Routing
    app.get('/view', function (request, response) {
        response.sendFile(path.join(__dirname, '/client/index.html'));
    });

    server.listen(port, function () {
        console.log('Старт сервера по адресу localhost:' + port);
        console.log('Старт просмотра по адресу localhost: ' + port + ' /view');
    });


    var player_move = {};
    var socketId;
    io.on('connection', function (socket) {

        socket.on('new player', function () {
            console.log('new player');
            socket.emit('ok');
        });
        socket.on('disconnect', function () {

        });

        socket.on('movement', function () {
            console.log('movement');
            console.log('player_move', player_move);
            if (player_move[socketId]) {
                let data = {
                    id: socketId,
                    x: player_move[socketId].x,
                    z: player_move[socketId].z,
                };
                socket.emit('ok', data);
            }
        });
        socket.on('phone_data', function (data) {
            console.log('phone_date', data);
            socketId = socket.id;
            player_move[socket.id] = {
                x: data.x,
                z: data.z
            };
        });


    // GET method route
        app.get('/', function (req, res) {
            res.send('Contact');
            console.log("est kontakt");
        });

    });

启动服务器时,由于几个用户可以同时控制角色,因为它们在同一个游戏空间中,所以每个人都必须拥有自己的角色。

0 个答案:

没有答案