Skip to content

Commit 1ad7834

Browse files
committed
implement Auth.Save()
1 parent 4362e49 commit 1ad7834

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

Assets/ColyseusClient.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ async void ConnectToServer ()
6262
await client.Auth.Login();
6363
var friends = await client.Auth.GetFriends();
6464

65+
// Update username
66+
client.Auth.Username = "Jake";
67+
await client.Auth.Save();
68+
6569
client.OnOpen += (object sender, EventArgs e) => {
6670
/* Update Demo UI */
6771
m_IdText.text = "id: " + client.Id;

Assets/Plugins/Colyseus/Auth.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,22 @@ public async Task<Auth> Login(NameValueCollection queryParams)
155155
return this;
156156
}
157157

158+
public async Task<Auth> Save()
159+
{
160+
var uploadData = new IndexedDictionary<string, string>();
161+
if (!string.IsNullOrEmpty(Username)) uploadData["username"] = Username;
162+
if (!string.IsNullOrEmpty(DisplayName)) uploadData["displayName"] = DisplayName;
163+
if (!string.IsNullOrEmpty(AvatarUrl)) uploadData["avatarUrl"] = AvatarUrl;
164+
if (!string.IsNullOrEmpty(Lang)) uploadData["lang"] = Lang;
165+
if (!string.IsNullOrEmpty(Location)) uploadData["location"] = Location;
166+
if (!string.IsNullOrEmpty(Timezone)) uploadData["timezone"] = Timezone;
167+
168+
var bodyString = Json.SerializeToString(uploadData);
169+
await Request<UserData>("PUT", "/auth", null, new UploadHandlerRaw(Encoding.UTF8.GetBytes(bodyString)));
170+
171+
return this;
172+
}
173+
158174
public async Task<UserData[]> GetFriends()
159175
{
160176
return await Request<UserData[]>("GET", "/friends/all");
@@ -211,7 +227,7 @@ public void Logout()
211227
PlayerPrefs.SetString("Token", Token);
212228
}
213229

214-
protected async Task<T> Request<T>(string method, string segments, NameValueCollection query = null)
230+
protected async Task<T> Request<T>(string method, string segments, NameValueCollection query = null, UploadHandlerRaw data = null)
215231
{
216232
if (query == null)
217233
{
@@ -231,6 +247,13 @@ protected async Task<T> Request<T>(string method, string segments, NameValueColl
231247
// FIXME: replacing "ws" with "http" is too hacky!
232248
req.url = uriBuilder.Uri.ToString().Replace("ws", "http");
233249

250+
// Send JSON on request body
251+
if (data != null)
252+
{
253+
req.uploadHandler = data;
254+
req.SetRequestHeader("Content-Type", "application/json");
255+
}
256+
234257
// Request headers
235258
req.SetRequestHeader("Accept", "application/json");
236259
if (HasToken) req.SetRequestHeader("Authorization", "Bearer " + Token);

Server/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import express from "express";
44
import { Server, serialize, FossilDeltaSerializer } from "colyseus";
55
import { DemoRoom } from "./DemoRoom";
66

7+
import socialRoutes from "@colyseus/social/express";
8+
79
const PORT = Number(process.env.PORT || 2567);
810

911
const app = express();
@@ -22,6 +24,8 @@ gameServer.register("demo", DemoRoom);
2224
class DemoRoomFossilDelta extends DemoRoom {}
2325
gameServer.register("demo_fossil", DemoRoomFossilDelta);
2426

27+
app.use("/", socialRoutes);
28+
2529
app.get("/something", function (req, res) {
2630
console.log("something!", process.pid);
2731
res.send("Hey!");

0 commit comments

Comments
 (0)