udamir / magx

Multiplayer game server framework
MIT License
40 stars 5 forks source link

400 Bad Request #7

Closed karneaud closed 2 years ago

karneaud commented 2 years ago

So I'm using cors and https to run the server( port 2567) while I run the game on a different server( port 3000).

While the logs show I'm able to create a room

Process 70: Get avalible rooms Find process for room: Current process load: {} All cluster pids [] Process 70 requested: Create room pullowar

The client side seems to return a

Request URL: https://localhost:2567/magx/rooms Request Method: POST Status Code: 400 Bad Request Remote Address: [2803:1500:e00:f22f:ba27:ebff:fede:91d0]:2567 Referrer Policy: strict-origin-when-cross-origin

with the following payload.

{"name":"pullowar","options":{}}

Did I need to set additional options for the room to be initialized?

karneaud commented 2 years ago

I notice that the authentication returns a token. Was I suppose to use the token when trying to create to join the room?

udamir commented 2 years ago

token is required for this API method. All available methods in api.ts file. I assume you are getting 400 error because the room with that name is not defined

The easiest way to start using magx server is via magx-client library, or you will have to implement all connection/communication logic yourself

karneaud commented 2 years ago

@udamir

Well based on your example I seem to be instantiating it correctly. Only difference is I had to use "https" in order for the monitor to work.

const createServer = require('https').createServer,
      fs = require('fs'),
  production = process.env.NODE_ENV || false,
  port = process.env.PORT || 2567,
  Server = require('magx').Server,
  server = createServer({
    key: fs.readFileSync(__dirname + '/../private.key'),
    cert: fs.readFileSync(__dirname + '/../private.crt')
  }),
  gameServer = new Server(server),
  PullOWarRoom = require('./rooms'),
      monitor = require('magx-monitor').monitor;

gameServer.define('pullowar', PullOWarRoom);
.....

and I am using the client correctly. Do I need to set the token manually? As I see it being passed in the headers.

const client = new Client({ address: match[3], port: SOCKET_PORT, secure: (/https/i).test(url), serializer: SchemaSerializer })
client.authenticate().then((r) => {
    console.log('auth', r) // i get the token
    return client.getRooms()
    }).then((rooms) => {
    console.log('rooms', rooms); // does not return anything
                                          return                       (ROOM_ID ? client.joinRoom(ROOM_ID) : client.createRoom('pullowar'))
    } ).then(room => {
        Room = room;
        setGameId( room.id)
 ...

}).catch(e => {
        console.log("JOIN ERROR", e); // prints 400 bad request 
    })

The game says its creating the room

Process 70: Get avalible rooms Find process for room: Current process load: {} All cluster pids [] Process 70 requested: Create room pullowar

However the monitor does not list it.

If I don't use CORs or HTTPS I get the other previous issues I logged in repo.

udamir commented 2 years ago

you need to call monitor(gameServer) to attach monitor to magx server. Monitor is very poor module, but it works with http as well as with https.

I tested magx server with https also, and did't have any issues.

udamir commented 2 years ago

If client don't join the room after creation it is closing automatically.