forked from Hezkore/koreTcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.monkey2
70 lines (48 loc) · 1.33 KB
/
server.monkey2
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
Namespace koreTcp
#Import "<std>"
#Import "<mojo>"
Using std..
Using mojo.app
#Import "inc/network.monkey2"
'Our own custom packet IDs
#Import "packetID.monkey2"
Global server:Server
Function Main()
New AppInstance
'Create a server on port 4012 for one client
server = New Server
If server.Listen( 4012, 1 ) Then
Print "Server @" + server.Address + " listening"
Else
Print "Unable to start listening server"
Endif
'Add hooks
server.GotPacketHook = GotPacket
server.NewClientHook = NewClient
server.DropClientHook = DropClient
App.Run()
End
Function GotPacket( packet:Packet, client:Server.Client )
Select packet.ID
Case PacketID.Key
Local keyPressed:UShort = packet.ReadUShort()
'Send the key back to the client
client.NewPacket( PacketID.Key )
client.Packet.WriteUShort( keyPressed )
client.SendPacket()
Print "Client #" + client.ID + " hit key " + keyPressed
Default
Print "Unknown packet: " + packet.ID
End
End
Function NewClient( client:Server.Client )
Print "Server accepted client @" + client.PeerAddress + " as #" + client.ID
'Print "Clients connected: "+client.Server.ClientCount
End
Function DropClient( client:Server.Client, reason:String )
If reason Then
Print "Client left #" + client.ID + " : " + reason
Else
Print "Client left #" + client.ID
Endif
End