-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConnector.cs
84 lines (71 loc) · 2.13 KB
/
Connector.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;
using Fleck;
using Net.DDP.Server.Interfaces;
namespace Net.DDP.Server
{
internal class Connector
{
private WebSocketServer _socket;
public List<IWebSocketConnection> Connections;
private readonly IServer _server;
private readonly string _ip;
private readonly int _port;
public Connector(IServer server, string ip, int port)
{
Connections = new List<IWebSocketConnection>();
_server = server;
_ip = ip;
_port = port;
}
public void Start()
{
_socket = new WebSocketServer(String.Format("ws://{0}:{1}", _ip, _port));
_socket.Start(socket =>
{
socket.OnOpen = () => _socket_onOpen(socket);
socket.OnClose = () => _socket_onClose(socket);
socket.OnMessage = (message) => _socket_onMessage(socket, message);
socket.OnPing = (bytes) => _socket_onPing(socket, bytes);
});
}
public void Disconnect()
{
foreach (var connection in Connections)
{
connection.Close();
}
}
public void SendPing()
{
foreach (var connection in Connections)
{
// TODO
}
}
private void _socket_onOpen(IWebSocketConnection socket)
{
Connections.Add(socket);
}
private void _socket_onPing(IWebSocketConnection socket, byte[] bytes)
{
// TODO
}
private void _socket_onClose(IWebSocketConnection socket)
{
Connections.Remove(socket);
}
public void SendMessage(IWebSocketConnection socket, string message)
{
socket.Send(message);
}
private void _socket_onMessage(IWebSocketConnection socket, string message)
{
_server.ProcessRequest(socket, message);
}
}
}