wtt
2024-09-20 d9b200d424889c069953ec084ef9d4dd6c4ff66b
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
using System;
using System.Collections.Generic;
using System.Text;
using WebSocketSharp;
using WebSocketSharp.Frame;
 
namespace Taobao.Top.Link.Channel.WebSocket
{
    /// <summary>websocket clientchannel via websocket-sharp impl
    /// </summary>
    public class WebSocketClientChannel : IClientChannel
    {
        private WebSocketSharp.WebSocket _socket;
        private ResetableTimer _timer;
        private EventHandler<ChannelContext> _onMessage;
        private EventHandler<ChannelContext> _onError;
        private EventHandler<ChannelClosedEventArgs> _onClosed;
 
        public EventHandler<ChannelContext> OnMessage
        {
            get { this.DelayPing(); return this._onMessage; }
            set { this._onMessage = value; }
        }
        public EventHandler<ChannelContext> OnError
        {
            get { this.DelayPing(); return this._onError; }
            set { this._onError = value; }
        }
        public EventHandler<ChannelClosedEventArgs> OnClosed
        {
            get { return this._onClosed; }
            set { this._onClosed = value; }
        }
 
        public Uri Uri { get; set; }
        public bool IsConnected { get { return this._socket.ReadyState == WsState.OPEN; } }
 
        public WebSocketClientChannel(WebSocketSharp.WebSocket socket)
        {
            this._socket = socket;
            this._onClosed += (o, e) =>
            {
                this.Close(e.Reason);
            };
        }
 
        public void Send(byte[] data)
        {
            this.CheckChannel();
            this._socket.Send(data);
        }
 
        public void Close(string reason)
        {
            this._socket.Close(CloseStatusCode.NORMAL, reason);
            if (this._timer != null)
            {
                this._timer.Cancel();
                this._timer = null;
#if DEBUG
                Console.WriteLine("TMC: Info@close: " + reason);
#endif
            }
        }
 
        public ResetableTimer HeartbeatTimer
        {
            set
            {
                this._timer = value;
                this._timer.Elapsed += (s, e) =>
                {
                    if (this.IsConnected)
                        //websocket-sharp's ping is sync
                        this._socket.Ping();
                };
            }
        }
 
        private void CheckChannel()
        {
            if (!this.IsConnected)
            {
                if (this._timer != null)
                    this._timer.Cancel();
                throw new LinkException("websocket channel closed");
            }
            this.DelayPing();
        }
        private void DelayPing()
        {
            try
            {
                if (this._timer != null)
                    this._timer.Delay();
            }
            catch (Exception)
            {
 
            }
        }
    }
}