SUPERSOCKET.CLIENTENGINE 簡單使用
江大沒有給ClientEngine的Demo,一直沒有找到其它的。。 自己從 websocket4net 項目中看了一些,然后寫了一個簡單的命令行的Client。
首先
引用 SuperSocket.ClientEngine.Core.dll和 SuperSocket.ClientEngine.Common.dll
然后
就可以使用ClientEngine了。
ClientEngine
我找了好久,只找到 AsyncTcpSession這么一個可以實例化的連接會話,那么,就用這個吧。
|
1
2
3
|
string
ip =
"127.0.0.1"
;
int
port = 12345;
AsyncTcpSession client =
new
AsyncTcpSession(
new
IPEndPoint(IPAddress.Parse(ip), port));
|
處理連接的事件
|
1
2
3
4
5
6
7
8
|
// 連接斷開事件
client.Closed += client_Closed;
// 收到服務器數據事件
client.DataReceived += client_DataReceived;
// 連接到服務器事件
client.Connected += client_Connected;
// 發生錯誤的處理
client.Error += client_Error;
|
處理函數
|
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
|
void
client_Error(
object
sender, ErrorEventArgs e)
{
Console.WriteLine(e.Exception.Message);
}
void
client_Connected(
object
sender, EventArgs e)
{
Console.WriteLine(
"連接成功"
);
}
void
client_DataReceived(
object
sender, DataEventArgs e)
{
string
msg = Encoding.Default.GetString(e.Data);
Console.WriteLine(msg);
}
void
client_Closed(
object
sender, EventArgs e)
{
Console.WriteLine(
"連接斷開"
);
}
public
void
Connect()
{
client.Connect();
string
loginCmd =
"LOGIN test"
;
byte
[] data = Encoding.Default.GetBytes(loginCmd);
client.Send(data, 0, data.Length);
}
|
連接到服務器
|
1
|
client.Connect();
|
向服務器發送數據
|
1
2
3
|
string
loginCmd =
"LOGIN test\r\n"
;
byte
[] data = Encoding.Default.GetBytes(loginCmd);
client.Send(data, 0, data.Length);
|
需要注意的是,因為服務器是使用的命令行協議,所以在數據末需要加上 \r\n。如果是使用其它協議,只是這里數據的結構發生變化。
如果服務器返回了數據,那么就可以在client_DataReceived函數中處理了。
具體的不怎么清楚,我也沒有測試,從名稱AsyncTcpSession來看,這個連接是異步的,也就是說,如果直接在 client.Connect()后執行 client.Send(xxxx) 很可能會異常,因為此時連接不一定建立了。所以,發送數據要在事件session_ConnectStarted調用后。
最后,就有了這樣的代碼:
|
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
|
using
SuperSocket.ClientEngine;
using
System;
using
System.Collections.Generic;
using
System.Net;
using
System.Text;
namespace
hyjiacan.com
{
public
class
SSClient
{
private
AsyncTcpSession client;
/// <summary>
///
/// </summary>
/// <param name="ip">服務器IP</param>
/// <param name="port">服務器端口</param>
public
SSClient(
string
ip,
int
port)
{
client =
new
AsyncTcpSession(
new
IPEndPoint(IPAddress.Parse(ip), port));
// 連接斷開事件
client.Closed += client_Closed;
// 收到服務器數據事件
client.DataReceived += client_DataReceived;
// 連接到服務器事件
client.Connected += client_Connected;
// 發生錯誤的處理
client.Error += client_Error;
}
void
client_Error(
object
sender, ErrorEventArgs e)
{
Console.WriteLine(e.Exception.Message);
}
void
client_Connected(
object
sender, EventArgs e)
{
Console.WriteLine(
"連接成功"
);
}
void
client_DataReceived(
object
sender, DataEventArgs e)
{
string
msg = Encoding.Default.GetString(e.Data);
Console.WriteLine(msg);
}
void
client_Closed(
object
sender, EventArgs e)
{
Console.WriteLine(
"連接斷開"
);
}
/// <summary>
/// 連接到服務器
/// </summary>
public
void
Connect()
{
client.Connect();
}
/// <summary>
/// 向服務器發命令行協議的數據
/// </summary>
/// <param name="key">命令名稱</param>
/// <param name="data">數據</param>
public
void
SendCommand(
string
key,
string
data)
{
if
(client.IsConnected)
{
byte
[] arr = Encoding.Default.GetBytes(
string
.Format(
"{0} {1}"
, key, data));
client.Send(arr, 0, arr.Length);
}
else
{
throw
new
InvalidOperationException(
"未建立連接"
);
}
}
}
}
|
using
SuperSocket.ClientEngine;
using
System;
using
System.Collections.Generic;
using
System.Net;
using
System.Text;
namespace
hyjiacan.com
{
public
class
SSClient
{
private
AsyncTcpSession client;
/// <summary>
///
/// </summary>
/// <param name="ip">服務器IP</param>
/// <param name="port">服務器端口</param>
public
SSClient(
string
ip,
int
port)
{
client =
new
AsyncTcpSession(
new
IPEndPoint(IPAddress.Parse(ip), port));
// 連接斷開事件
client.Closed += client_Closed;
// 收到服務器數據事件
client.DataReceived += client_DataReceived;
// 連接到服務器事件
client.Connected += client_Connected;
// 發生錯誤的處理
client.Error += client_Error;
}
void
client_Error(
object
sender, ErrorEventArgs e)
{
Console.WriteLine(e.Exception.Message);
}
void
client_Connected(
object
sender, EventArgs e)
{
Console.WriteLine(
"連接成功"
);
}
void
client_DataReceived(
object
sender, DataEventArgs e)
{
string
msg = Encoding.Default.GetString(e.Data);
Console.WriteLine(msg);
}
void
client_Closed(
object
sender, EventArgs e)
{
Console.WriteLine(
"連接斷開"
);
}
/// <summary>
/// 連接到服務器
/// </summary>
public
void
Connect()
{
client.Connect();
}
/// <summary>
/// 向服務器發命令行協議的數據
/// </summary>
/// <param name="key">命令名稱</param>
/// <param name="data">數據</param>
public
void
SendCommand(
string
key,
string
data)
{
if
(client.IsConnected)
{
byte
[] arr = Encoding.Default.GetBytes(
string
.Format(
"{0} {1}"
, key, data));
client.Send(arr, 0, arr.Length);
}
else
{
throw
new
InvalidOperationException(
"未建立連接"
);
}
}
}
}
