-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebSocketService.cs
More file actions
120 lines (96 loc) · 3.43 KB
/
WebSocketService.cs
File metadata and controls
120 lines (96 loc) · 3.43 KB
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
117
118
119
120
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.Common;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketEngine;
using SuperSocket.SocketEngine.Configuration;
using SuperWebSocket;
using CoAP;
namespace Coap.Proxy
{
class WebSocketService
{
private List<WebSocketSession> m_Sessions = new List<WebSocketSession>();
private object m_SessionSyncRoot = new object();
private IBootstrap m_Bootstrap;
private int port=0;
public List<WebSocketSession> SessionList
{
get { return m_Sessions; }
}
public WebSocketService(int port)
{
this.port = port;
//this.listener = new TcpListener(IPAddress.IPv6Any, port);
}
void socketServer_NewMessageReceived(WebSocketSession session, string e)
{
SendToAll(session.Cookies["name"] + ": " + e);
}
public void StartSuperWebSocketByProgramming()
{
var socketServer = new WebSocketServer();
socketServer.NewMessageReceived += new SessionHandler<WebSocketSession, string>(socketServer_NewMessageReceived);
socketServer.NewSessionConnected += socketServer_NewSessionConnected;
socketServer.SessionClosed += socketServer_SessionClosed;
socketServer.Setup(port);
m_Bootstrap = new DefaultBootstrap(new RootConfig(), new IWorkItem[] { socketServer });
m_Bootstrap.Start();
}
void socketServer_NewSessionConnected(WebSocketSession session)
{
lock (m_SessionSyncRoot)
m_Sessions.Add(session);
SendToObserved("System: observe connected",session);
}
public void socketServer_SessionClosed(WebSocketSession session, CloseReason reason)
{
lock (m_SessionSyncRoot)
m_Sessions.Remove(session);
if (reason == CloseReason.ServerShutdown)
return;
Console.WriteLine(session.ToString()+"connect disposed");
//SendToAll("System: " + session.Cookies["name"] + " disconnected");
}
public void SendToAll(string message)
{
lock (m_SessionSyncRoot)
{
foreach (var s in m_Sessions)
{
s.Send(message);
}
}
}
public void SendToObserved(string message,WebSocketSession s)
{
s.Send(message);
}
void Application_End(object sender, EventArgs e)
{
if (m_Bootstrap != null)
m_Bootstrap.Stop();
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
public void Session_End()
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
lock (m_SessionSyncRoot)
m_Sessions.Clear();
}
}
}