Skip to content

Commit

Permalink
add example sending a typed message to the server.
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Feb 29, 2020
1 parent 7ffecbc commit 1954d98
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 18 deletions.
12 changes: 12 additions & 0 deletions Assets/ColyseusClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class CustomRoomAvailable : RoomAvailable
public Metadata metadata;
}

class CustomData
{
public int integer;
public string str;
}

public class ColyseusClient : MonoBehaviour {

// UI Buttons are attached through Unity Inspector
Expand Down Expand Up @@ -201,6 +207,12 @@ void SendMessage()
if (room != null)
{
room.Send("move_right");

// Sending typed data to the server
room.Send(new CustomData() {
integer = 100,
str = "Hello world!"
});
}
else
{
Expand Down
3 changes: 1 addition & 2 deletions Assets/Editor/ColyseusTests/ClientComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public class ClientComponent : MonoBehaviour
public async void Start () {
client = new Client("ws://localhost:2567");

room = await client.Join<State>("demo");
await room.Connect();
room = await client.JoinOrCreate<State>("demo");

// OnApplicationQuit();
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Editor/ColyseusTests/RoomTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class RoomTest {
ClientComponent component;

[UnityTest]
public async Task TestConnection()
public async void TestConnection()
{
var gameObject = new GameObject();
component = gameObject.AddComponent<ClientComponent>();
Expand Down
2 changes: 1 addition & 1 deletion Assets/Enemy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// THIS FILE HAS BEEN GENERATED AUTOMATICALLY
// DO NOT CHANGE IT MANUALLY UNLESS YOU KNOW WHAT YOU'RE DOING
//
// GENERATED USING @colyseus/schema 0.5.5
// GENERATED USING @colyseus/schema 0.5.32
//

using Colyseus.Schema;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// THIS FILE HAS BEEN GENERATED AUTOMATICALLY
// DO NOT CHANGE IT MANUALLY UNLESS YOU KNOW WHAT YOU'RE DOING
//
// GENERATED USING @colyseus/schema 0.5.5
// GENERATED USING @colyseus/schema 0.5.32
//

using Colyseus.Schema;
Expand Down
Binary file modified Assets/ExampleScene.unity
Binary file not shown.
2 changes: 1 addition & 1 deletion Assets/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// THIS FILE HAS BEEN GENERATED AUTOMATICALLY
// DO NOT CHANGE IT MANUALLY UNLESS YOU KNOW WHAT YOU'RE DOING
//
// GENERATED USING @colyseus/schema 0.5.5
// GENERATED USING @colyseus/schema 0.5.32
//

using Colyseus.Schema;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// THIS FILE HAS BEEN GENERATED AUTOMATICALLY
// DO NOT CHANGE IT MANUALLY UNLESS YOU KNOW WHAT YOU'RE DOING
//
// GENERATED USING @colyseus/schema 0.5.5
// GENERATED USING @colyseus/schema 0.5.32
//

using Colyseus.Schema;
Expand Down
39 changes: 31 additions & 8 deletions Assets/Plugins/Colyseus/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public Client (string endpoint)
Auth = new Auth(Endpoint.Uri);
}

/**
* Join
*/
public async Task<Room<T>> JoinOrCreate<T>(string roomName, Dictionary<string, object> options = null, Dictionary<string, string> headers = null)
{
return await CreateMatchMakeRequest<T>("joinOrCreate", roomName, options, headers);
Expand Down Expand Up @@ -99,15 +102,35 @@ public async Task<Room<T>> Reconnect<T>(string roomId, string sessionId, Diction
return await CreateMatchMakeRequest<T>("joinById", roomId, options, headers);
}

//public async Task<Room<IndexedDictionary<string, object>>> Join(string roomName, Dictionary<string, object> options = null)
//{
// return await Join<IndexedDictionary<string, object>>(roomName, options);
//}
//
// Fossil-Delta versions for joining the state
//
public async Task<Room<IndexedDictionary<string, object>>> JoinOrCreate(string roomName, Dictionary<string, object> options = null, Dictionary<string, string> headers = null)
{
return await CreateMatchMakeRequest<IndexedDictionary<string, object>>("joinOrCreate", roomName, options, headers);
}

public async Task<Room<IndexedDictionary<string, object>>> Create(string roomName, Dictionary<string, object> options = null, Dictionary<string, string> headers = null)
{
return await CreateMatchMakeRequest<IndexedDictionary<string, object>>("create", roomName, options, headers);
}

public async Task<Room<IndexedDictionary<string, object>>> Join(string roomName, Dictionary<string, object> options = null, Dictionary<string, string> headers = null)
{
return await CreateMatchMakeRequest<IndexedDictionary<string, object>>("join", roomName, options, headers);
}

//public async Task<Room<IndexedDictionary<string, object>>> ReJoin (string roomName, string sessionId)
//{
// return await ReJoin<IndexedDictionary<string, object>>(roomName, sessionId);
//}
public async Task<Room<IndexedDictionary<string, object>>> JoinById(string roomId, Dictionary<string, object> options = null, Dictionary<string, string> headers = null)
{
return await CreateMatchMakeRequest<IndexedDictionary<string, object>>("joinById", roomId, options, headers);
}

public async Task<Room<IndexedDictionary<string, object>>> Reconnect(string roomId, string sessionId, Dictionary<string, string> headers = null)
{
Dictionary<string, object> options = new Dictionary<string, object>();
options.Add("sessionId", sessionId);
return await CreateMatchMakeRequest<IndexedDictionary<string, object>>("joinById", roomId, options, headers);
}

public async Task<RoomAvailable[]> GetAvailableRooms(string roomName = "")
{
Expand Down
4 changes: 2 additions & 2 deletions Assets/Plugins/Colyseus/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ private void Initialize()

public async Task Send(object[] data)
{
var serializationOutput = new MemoryStream ();
MsgPack.Serialize (data, serializationOutput);
var serializationOutput = new MemoryStream();
MsgPack.Serialize(data, serializationOutput, SerializationOptions.SuppressTypeInformation);

byte[] packedData = serializationOutput.ToArray ();

Expand Down
2 changes: 1 addition & 1 deletion Assets/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// THIS FILE HAS BEEN GENERATED AUTOMATICALLY
// DO NOT CHANGE IT MANUALLY UNLESS YOU KNOW WHAT YOU'RE DOING
//
// GENERATED USING @colyseus/schema 0.5.5
// GENERATED USING @colyseus/schema 0.5.32
//

using Colyseus.Schema;
Expand Down
2 changes: 2 additions & 0 deletions Server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Server } from "colyseus";
import { DemoRoom } from "./DemoRoom";

import socialRoutes from "@colyseus/social/express";
import { FossilDeltaTestRoom } from "./FossilDeltaTestRoom";

const PORT = Number(process.env.PORT || 2567);

Expand All @@ -24,6 +25,7 @@ const gameServer = new Server({

// Register DemoRoom as "demo"
gameServer.define("demo", DemoRoom);
gameServer.define("fossildelta", FossilDeltaTestRoom);

app.use("/", socialRoutes);

Expand Down

0 comments on commit 1954d98

Please sign in to comment.