Skip to content

Commit 28bc5f9

Browse files
committed
Ignorance 1.3.4 pre-release
1 parent 16a9704 commit 28bc5f9

File tree

9 files changed

+387
-213
lines changed

9 files changed

+387
-213
lines changed

Assets/Demo/PongChamp.unity

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,7 @@ MonoBehaviour:
959959
transport: {fileID: 1886246554}
960960
networkAddress: localhost
961961
maxConnections: 4
962+
authenticator: {fileID: 0}
962963
playerPrefab: {fileID: 1240244544407914, guid: b1651eaf8c7564a1c86031dfbb8a7b28,
963964
type: 3}
964965
autoCreatePlayer: 1
@@ -984,7 +985,7 @@ MonoBehaviour:
984985
OnClientDataReceived:
985986
m_PersistentCalls:
986987
m_Calls: []
987-
m_TypeName: Mirror.UnityEventArraySegment, Mirror, Version=0.0.0.0, Culture=neutral,
988+
m_TypeName: Mirror.ClientDataReceivedEvent, Mirror, Version=0.0.0.0, Culture=neutral,
988989
PublicKeyToken=null
989990
OnClientError:
990991
m_PersistentCalls:
@@ -1003,7 +1004,7 @@ MonoBehaviour:
10031004
OnServerDataReceived:
10041005
m_PersistentCalls:
10051006
m_Calls: []
1006-
m_TypeName: Mirror.UnityEventIntArraySegment, Mirror, Version=0.0.0.0, Culture=neutral,
1007+
m_TypeName: Mirror.ServerDataReceivedEvent, Mirror, Version=0.0.0.0, Culture=neutral,
10071008
PublicKeyToken=null
10081009
OnServerError:
10091010
m_PersistentCalls:
@@ -1014,7 +1015,7 @@ MonoBehaviour:
10141015
m_PersistentCalls:
10151016
m_Calls: []
10161017
m_TypeName: Mirror.UnityEventInt, Mirror, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
1017-
DebugEnabled: 0
1018+
DebugEnabled: 1
10181019
ServerBindAll: 1
10191020
ServerBindAddress: 127.0.0.1
10201021
CommunicationPort: 7777
@@ -1097,7 +1098,7 @@ PrefabInstance:
10971098
- target: {fileID: 440601958556739609, guid: 5339554c46006dd4e91cae9ff34c095d,
10981099
type: 3}
10991100
propertyPath: m_SceneId
1100-
value: 1035721996
1101+
value: 3089825571
11011102
objectReference: {fileID: 0}
11021103
- target: {fileID: 440842259441939327, guid: 5339554c46006dd4e91cae9ff34c095d,
11031104
type: 3}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
namespace Mirror.Authenticators
5+
{
6+
[AddComponentMenu("Network/Authenticators/BasicAuthenticator")]
7+
public class BasicAuthenticator : NetworkAuthenticator
8+
{
9+
[Header("Custom Properties")]
10+
11+
// set these in the inspector
12+
public string username;
13+
public string password;
14+
15+
public class AuthRequestMessage : MessageBase
16+
{
17+
// use whatever credentials make sense for your game
18+
// for example, you might want to pass the accessToken if using oauth
19+
public string authUsername;
20+
public string authPassword;
21+
}
22+
23+
public class AuthResponseMessage : MessageBase
24+
{
25+
public byte code;
26+
public string message;
27+
}
28+
29+
public override void OnStartServer()
30+
{
31+
// register a handler for the authentication request we expect from client
32+
NetworkServer.RegisterHandler<AuthRequestMessage>(OnAuthRequestMessage, false);
33+
}
34+
35+
public override void OnStartClient()
36+
{
37+
// register a handler for the authentication response we expect from server
38+
NetworkClient.RegisterHandler<AuthResponseMessage>(OnAuthResponseMessage, false);
39+
}
40+
41+
public override void OnServerAuthenticate(NetworkConnection conn)
42+
{
43+
// do nothing...wait for AuthRequestMessage from client
44+
}
45+
46+
public override void OnClientAuthenticate(NetworkConnection conn)
47+
{
48+
AuthRequestMessage authRequestMessage = new AuthRequestMessage
49+
{
50+
authUsername = username,
51+
authPassword = password
52+
};
53+
54+
NetworkClient.Send(authRequestMessage);
55+
}
56+
57+
public void OnAuthRequestMessage(NetworkConnection conn, AuthRequestMessage msg)
58+
{
59+
Debug.LogFormat("Authentication Request: {0} {1}", msg.authUsername, msg.authPassword);
60+
61+
// check the credentials by calling your web server, database table, playfab api, or any method appropriate.
62+
if (msg.authUsername == username && msg.authPassword == password)
63+
{
64+
// create and send msg to client so it knows to proceed
65+
AuthResponseMessage authResponseMessage = new AuthResponseMessage
66+
{
67+
code = 100,
68+
message = "Success"
69+
};
70+
71+
NetworkServer.SendToClient(conn.connectionId, authResponseMessage);
72+
73+
// Invoke the event to complete a successful authentication
74+
base.OnServerAuthenticated.Invoke(conn);
75+
}
76+
else
77+
{
78+
// create and send msg to client so it knows to disconnect
79+
AuthResponseMessage authResponseMessage = new AuthResponseMessage
80+
{
81+
code = 200,
82+
message = "Invalid Credentials"
83+
};
84+
85+
NetworkServer.SendToClient(conn.connectionId, authResponseMessage);
86+
87+
// must set NetworkConnection isAuthenticated = false
88+
conn.isAuthenticated = false;
89+
90+
// disconnect the client after 1 second so that response message gets delivered
91+
Invoke(nameof(conn.Disconnect), 1);
92+
}
93+
}
94+
95+
public void OnAuthResponseMessage(NetworkConnection conn, AuthResponseMessage msg)
96+
{
97+
if (msg.code == 100)
98+
{
99+
Debug.LogFormat("Authentication Response: {0}", msg.message);
100+
101+
// Invoke the event to complete a successful authentication
102+
base.OnClientAuthenticated.Invoke(conn);
103+
}
104+
else
105+
{
106+
Debug.LogErrorFormat("Authentication Response: {0}", msg.message);
107+
108+
// Set this on the client for local reference
109+
conn.isAuthenticated = false;
110+
111+
// disconnect the client
112+
conn.Disconnect();
113+
}
114+
}
115+
}
116+
}

Assets/Mirror/Authenticators/BasicAuthenticator.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "Mirror.Authenticators",
3+
"references": [
4+
"Mirror"
5+
],
6+
"optionalUnityReferences": [],
7+
"includePlatforms": [],
8+
"excludePlatforms": [],
9+
"allowUnsafeCode": false,
10+
"overrideReferences": false,
11+
"precompiledReferences": [],
12+
"autoReferenced": true,
13+
"defineConstraints": []
14+
}

Assets/Mirror/Authenticators/Mirror.Authenticators.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)