≡
Products
▼
Support
▼
Company
▼
Forum
Ambiera Forum
Discussions, Help and Support.
Create a new account
,
Search
, or Login:
Create reply in
Question on multiplayer: are WebSockets useful for creating a multiplayer game?
Posted by:
(you are not logged in)
Enter the missing letter in: "Inte?national"
(you are not logged in)
Text:
[quote][b][/b] wrote: http is not that bad tbh. The main thing you should not send a new request every frame. Instead you save your game state send it to a server every 100 ms or so. For example, let's say you have a simple game: two players p1 and p2. They can shoot to each other, the one who's not dead wins. What can you do: Server side. You need to create a simple server app using php, nodejs, .net or whatever you like to use. It will not be a real "server" just a tool for interchanging the data between clients (i.e. players). In-game it will use only 1 endpoint, call it update. There should obviously be others which will allow you to establish a connection, create a lobby and etc, but I think you will figure it out. Client side. You need to send your player data to the other guy. There are multiple things to send: your character rotation, position, and animation and also data which could be described as events: player fires a gun, player takes damage, etc. So: For player data (position, rotation, animation): 1. Client 1 side: you should have a function which gets your local player character position, rotation and current animation and sends it to the server every 100 ms (/update endpoint). You also need to assign both client IDs (1 and 2 for example); 2. Server side: as server receives this data it saves it to a variable matched by player id. [code]var player1data = { posx: 1, posy: 2, posz: 3 rotx: ... etc } var player2data = { posx: 10, posy: 2, posz: -23 ...etc } [/code] 3. Client 2 side: client two sends it's data the same way as client 1 (/update). In response to this call it receives player 1 data. 4. You now have player 1 data on player 2 client. But you can't directly apply it,since we're only sending it 100 ms and moving/rotation would be jumpy in that case. For that on client you create a states array, which stores player 1 data along with the time than data was received. Something like: [code] // an array to store your states var stateArray = []; // update endpoint call function // you send playerId and playerData to the server using url parameters ccbDoHttpRequest('yourserverhostorip/update?playerId=' + playerId + '&data' + playerData, function(receivedData) { // received data from the remote player // parse receivedData to js object somehow so you can // access remote player position, rotation and animation // parse is just to mock - it's not a real function var parsedData = parse(receivedData); // save remote player data snapshot time parsedData[time] = (new Date()).getTime(); stateArray.push(parsedData); }) [/code][/quote]
Possible Codes
Feature
Code
Link
[url] www.example.com [/url]
Bold
[b]bold text[/b]
Image
[img]http://www.example.com/image.jpg[/img]
Quote
[quote]quoted text[/quote]
Code
[code]source code[/code]
Emoticons
Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact