forked from programistka/WebSocketsDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.cs
116 lines (94 loc) · 3.51 KB
/
Server.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperWebSocket;
using SuperSocket.SocketBase;
namespace WebSocketsServerDemo
{
class Server
{
private WebSocketServer appServer;
public void Setup()
{
Console.WriteLine("Press any key to start the WebSocketServer!");
Console.ReadKey();
Console.WriteLine();
appServer = new WebSocketServer();
if (!appServer.Setup(2012)) //Setup with listening port
{
Console.WriteLine("Failed to setup!");
Console.ReadKey();
return;
}
appServer.NewSessionConnected += new SessionHandler<WebSocketSession>(appServer_NewSessionConnected);
appServer.SessionClosed += new SessionHandler<WebSocketSession, CloseReason>(appServer_SessionClosed);
appServer.NewMessageReceived += new SessionHandler<WebSocketSession, string>(appServer_NewMessageReceived);
Console.WriteLine();
}
public void Start()
{
if (!appServer.Start())
{
Console.WriteLine("Failed to start!");
Console.ReadKey();
return;
}
Console.WriteLine("The server started successfully! Press any key to see application options.");
Console.ReadKey();
ShowAvailableOptions();
char keyStroked;
while (true)
{
keyStroked = Console.ReadKey().KeyChar;
if (keyStroked.Equals('q'))
{
Stop();
return;
}
if (keyStroked.Equals('s'))
{
Console.WriteLine();
Console.WriteLine("Put here your message to clients: ");
string message = Console.ReadLine();
foreach (WebSocketSession session in appServer.GetAllSessions())
{
session.Send("Message to client: " + message);
}
}
ShowAvailableOptions();
continue;
}
}
public void Stop()
{
appServer.Stop();
Console.WriteLine();
Console.WriteLine("The server was stopped!");
}
public void ShowAvailableOptions()
{
Console.WriteLine();
Console.WriteLine("Available options: ");
Console.WriteLine("Press 'q' key to stop the server.");
Console.WriteLine("Press 's' key to send message to client.");
}
private void appServer_NewMessageReceived(WebSocketSession session, string message)
{
Console.WriteLine("Client said: " + message);
//Send the received message back
session.Send("Server responded back: " + message);
}
private void appServer_NewSessionConnected(WebSocketSession session)
{
Console.WriteLine();
Console.WriteLine("New session connected! Sessions counter: " + appServer.SessionCount);
session.Send("Hello new client!");
}
private void appServer_SessionClosed(WebSocketSession session, CloseReason value)
{
Console.WriteLine();
Console.WriteLine("Client disconnected! Sessions counter: " + appServer.SessionCount);
}
}
}