NetClient.cs
3.97 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.ClientEngine;
using WebSocket4Net;
namespace NetLib
{
public class NetClient
{
WebSocket ClientObj = null;
public delegate void DelSessionConnected();
public event DelSessionConnected SessionConnected;
public delegate void DelSessionClosed();
public event DelSessionClosed SessionClosed;
public delegate void DelWebsocketError(Exception e);
public event DelWebsocketError WebsocketError;
public delegate void DelMessageReceived(string value);
public event DelMessageReceived MessageReceived;
public delegate void DelDataReceived(byte[] value);
public event DelDataReceived DataReceived;
public delegate void DelSessionErr(Exception ex);
public event DelSessionErr SessionErr;
public void Setup(string ipAddress, int port, List<KeyValuePair<string, string>> lstPar)
{
if (ClientObj != null)
{
Close();
ClientObj.Opened -= Websocket_Opened;
ClientObj.Error -= Websocket_Error;
ClientObj.Closed -= Websocket_Closed;
ClientObj.MessageReceived -= Websocket_MessageReceived;
ClientObj.DataReceived -= Websocket_DataReceived;
ClientObj.Dispose();
}
ClientObj = new WebSocket("ws://" + ipAddress + ":" + port + "/", cookies: lstPar);
ClientObj.Opened += Websocket_Opened;
ClientObj.Error += Websocket_Error;
ClientObj.Closed += Websocket_Closed;
ClientObj.MessageReceived += Websocket_MessageReceived;
ClientObj.DataReceived += Websocket_DataReceived;
}
private void Websocket_Opened(object sender, EventArgs e)
{
SessionConnected?.Invoke();
}
private void Websocket_Closed(object sender, EventArgs e)
{
SessionClosed?.Invoke();
}
private void Websocket_Error(object sender, ErrorEventArgs e)
{
string sErr = e.Exception + "";
WebsocketError?.Invoke(e.Exception);
}
private void Websocket_MessageReceived(object sender, MessageReceivedEventArgs e)
{
MessageReceived?.Invoke(e.Message);
}
private void Websocket_DataReceived(object sender, DataReceivedEventArgs e)
{
DataReceived?.Invoke(e.Data);
}
public bool IsOpen
{
get
{
return ((ClientObj != null) && (ClientObj.State == WebSocketState.Open));
}
}
public void Open()
{
if (ClientObj != null)
ClientObj.Open();
}
public void Close()
{
try
{
if ((ClientObj != null) && (ClientObj.State != WebSocketState.Closed))
{
ClientObj.Close();
}
}
catch(Exception ex)
{
string sErr = ex + "";
}
}
/// <summary>
/// 杨斌 2020-05-17
/// </summary>
public void Send(string msg)
{
try
{
if (ClientObj != null)
ClientObj.Send(msg);
}
catch (Exception ex)
{
string sErr = "" + ex;
SessionErr?.Invoke(ex);
}
}
/// <summary>
/// 杨斌 2020-05-17
/// </summary>
public void Send(byte[] data)
{
try
{
if (ClientObj != null)
ClientObj.Send(data, 0, data.Length);
}
catch (Exception ex)
{
string sErr = "" + ex;
SessionErr?.Invoke(ex);
}
}
}
}