This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
SampleClient.cs
91 lines (80 loc) · 3.55 KB
/
SampleClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using UnityEngine;
public class SampleClient : MonoBehaviour, IRakClient
{
public string playerName = "Player";
void Awake()
{
/* Registering the interface for processing packets and receiving events when connecting and disconnecting clients */
RakClient.RegisterInterface(this);
}
void OnGUI()
{
GUILayout.BeginHorizontal();
GUILayout.Space(450);
GUILayout.BeginVertical();
if(RakClient.State == ClientState.IS_DISCONNECTED)
{
if(GUILayout.Button("Connect to server"))
{
RakClient.Connect("127.0.0.1", 7777);
}
}
else if(RakClient.State == ClientState.IS_CONNECTING)
{
GUILayout.Label("Client connecting...");
}
else
{
if (GUILayout.Button("Disconnect"))
{
RakClient.Disconnect();
}
GUILayout.Box("Ping: "+RakClient.Ping);
GUILayout.Box("Average ping: "+RakClient.AveragePing);
GUILayout.Box("Lowest ping: "+RakClient.LowestPing);
GUILayout.Box("Connection time: "+RakClient.Statistics.ConnectionTime());
GUILayout.Box("Bytes received: "+RakClient.Statistics.GetStatsTotal(RNSPerSecondMetrics.ACTUAL_BYTES_RECEIVED));
GUILayout.Box("Bytes sended: "+RakClient.Statistics.GetStatsTotal(RNSPerSecondMetrics.ACTUAL_BYTES_SENT));
}
GUILayout.EndVertical();
GUILayout.EndHorizontal();
}
void IRakClient.OnConnecting(string address, ushort port, string password)
{
Debug.Log("[SampleClient] Connecting to " + address + ":" + port);
}
void IRakClient.OnConnected(string address, ushort port, string password)
{
Debug.Log("[SampleClient] Connected to "+address+":"+port);
}
void IRakClient.OnDisconnected(DisconnectReason reason, string message)
{
Debug.Log("[SampleClient] Disconnected "+reason + " -> "+message);
}
void IRakClient.OnReceived(byte packet_id, uint packet_size, BitStream bitStream, ulong local_time)
{
switch ((SamplePacketID)packet_id)
{
/* The server requests information about the client, and we will send him the name of the player */
case SamplePacketID.CLIENT_DATA_REQUEST:
/* Recommended design when writing data for sending */
using (PooledBitStream bsOut = PooledBitStream.GetBitStream())
{
/*
* Always write the first byte as the packet number before sending data! (range from 134 to 255),
* this is necessary so that the receiving data knows how to process it
*/
bsOut.Write((byte)SamplePacketID.CLIENT_DATA_REPLY);
bsOut.Write(playerName);
/* We send data to the server with priority for immediate sending, reliable transmission over channel 0 */
RakClient.Send(bsOut, PacketPriority.IMMEDIATE_PRIORITY, PacketReliability.RELIABLE, 0);
}
break;
/* The server notify that the data has been processed */
case SamplePacketID.CLIENT_DATA_ACCEPTED:
playerName = bitStream.ReadString();//read the changed name of the player by the server
Debug.Log("[SampleClient] Client data accepted by server... My name is "+playerName);
break;
}
}
}