SUPERSOCKET 客户端

时间:2023-03-10 02:37:18
SUPERSOCKET 客户端

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("未建立连接");
            }
        }
    }
}