Use Socket.set and Socket.get to access player information
Hundred Cattle Information Technology Bainiu.ltd organized and published in the blog Park
Socket.Get(' Playerinfo ', function (Err,Player) { If(Player== Null) {Console.Log("[New player]",Data.Name); VarPlayer= New Player(Socket,Data); } Else {Console.Log("[Wait player visit again]",Data.Name);Socket.Emit(' Error ', "You have registered"); Return ; }Socket.Set(' Playerinfo ',Player, function () {Usersws.Push(Player);Console.Log( "[Wait player Numer]" ,usersws. Length//this in if the player status and quantity meet the requirements can start the game physiccreatephysicworld (p1 P2); //notify client launch game .............. //set }); //get}) //socket.on
Forward and process player input
Socket.On(' Sync ', function (Data) {Socket.Get(' Playerinfo ', function (Err,Player) { If(Player!= Null) { var target = Usersws[i].socket; VarThe=Player.The; For(VarI=0;I<The.Players.Length;I++) { If(The.Players[I].Id==Player.Id) { Console.log ("This is the MSG from ID:", room.players[i].id) } Else {The.Players[I].Socket.Emit(' Sync ',Data); //console.log ("Send to ID:", room.players[i].id) } //room.players[i].socket.emit (' sync ', data); } }//if { Socket. ( ' ERROR ' , "no Session" } }); //get }); //socket.on
}); Network.on
Define a simple Player object
function Player(Socket,Data){VarNewplayer= {Socket: Socket, Id: Player_next_id++, Tanktype: 0, Name: Data., Ismain: Data.< Span class= "PLN" >ismain, Ip: Socketremoteaddress, Status: ' not ready '
Isai:false, room:null}; Newplayer.proto = Player.prototype; return newplayer; }
Define a game Room object
function The(Title){VarNewroom= {Id:room_next_id++,Is_in_game: False, Decide if the game to change the room has startedPlayers: [], Map: 0 , Title: Title, Game: null}; room_list.newroom. __proto__ = room.; return Newroom
}
The final speech: I am a novice, the article is not easy to interact with the text. Poor place, but also look at the Master of Light to shoot bricks.
Nodejs Build Multiplayer game server (beginner)