|
| 1 | +using Quobject.EngineIoClientDotNet.Modules; |
| 2 | +using Quobject.EngineIoClientDotNet.Parser; |
| 3 | +using System; |
| 4 | +using System.Net; |
| 5 | +using System.Collections.Generic; |
| 6 | +using WebSocket4Net; |
| 7 | +using SuperSocket.ClientEngine.Proxy; |
| 8 | + |
| 9 | +namespace Quobject.EngineIoClientDotNet.Client.Transports |
| 10 | +{ |
| 11 | + public class WebSocket : Transport |
| 12 | + { |
| 13 | + public static readonly string NAME = "websocket"; |
| 14 | + |
| 15 | + private WebSocket4Net.WebSocket ws; |
| 16 | + private List<KeyValuePair<string, string>> Cookies; |
| 17 | + private List<KeyValuePair<string, string>> MyExtraHeaders; |
| 18 | + |
| 19 | + public WebSocket(Options opts) |
| 20 | + : base(opts) |
| 21 | + { |
| 22 | + Name = NAME; |
| 23 | + Cookies = new List<KeyValuePair<string, string>>(); |
| 24 | + foreach (var cookie in opts.Cookies) |
| 25 | + { |
| 26 | + Cookies.Add(new KeyValuePair<string, string>(cookie.Key, cookie.Value)); |
| 27 | + } |
| 28 | + MyExtraHeaders = new List<KeyValuePair<string, string>>(); |
| 29 | + foreach (var header in opts.ExtraHeaders) |
| 30 | + { |
| 31 | + MyExtraHeaders.Add(new KeyValuePair<string, string>(header.Key, header.Value)); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + protected override void DoOpen() |
| 36 | + { |
| 37 | + var log = LogManager.GetLogger(Global.CallerName()); |
| 38 | + log.Info("DoOpen uri =" + this.Uri()); |
| 39 | + |
| 40 | + ws = new WebSocket4Net.WebSocket(this.Uri(), String.Empty, Cookies, MyExtraHeaders) |
| 41 | + { |
| 42 | + EnableAutoSendPing = false |
| 43 | + }; |
| 44 | + if (ServerCertificate.Ignore) |
| 45 | + { |
| 46 | + var security = ws.Security; |
| 47 | + |
| 48 | + if (security != null) |
| 49 | + { |
| 50 | + security.AllowUnstrustedCertificate = true; |
| 51 | + security.AllowNameMismatchCertificate = true; |
| 52 | + } |
| 53 | + } |
| 54 | + ws.Opened += ws_Opened; |
| 55 | + ws.Closed += ws_Closed; |
| 56 | + ws.MessageReceived += ws_MessageReceived; |
| 57 | + ws.DataReceived += ws_DataReceived; |
| 58 | + ws.Error += ws_Error; |
| 59 | + |
| 60 | + var destUrl = new UriBuilder(this.Uri()); |
| 61 | + if (this.Secure) |
| 62 | + destUrl.Scheme = "https"; |
| 63 | + else |
| 64 | + destUrl.Scheme = "http"; |
| 65 | + var useProxy = !WebRequest.DefaultWebProxy.IsBypassed(destUrl.Uri); |
| 66 | + if (useProxy) |
| 67 | + { |
| 68 | + var proxyUrl = WebRequest.DefaultWebProxy.GetProxy(destUrl.Uri); |
| 69 | + var proxy = new HttpConnectProxy(new DnsEndPoint(proxyUrl.Host, proxyUrl.Port), destUrl.Host); |
| 70 | + ws.Proxy = proxy; |
| 71 | + } |
| 72 | + ws.Open(); |
| 73 | + } |
| 74 | + |
| 75 | + void ws_DataReceived(object sender, DataReceivedEventArgs e) |
| 76 | + { |
| 77 | + var log = LogManager.GetLogger(Global.CallerName()); |
| 78 | + log.Info("ws_DataReceived " + e.Data); |
| 79 | + this.OnData(e.Data); |
| 80 | + } |
| 81 | + |
| 82 | + private void ws_Opened(object sender, EventArgs e) |
| 83 | + { |
| 84 | + var log = LogManager.GetLogger(Global.CallerName()); |
| 85 | + log.Info("ws_Opened " + ws.SupportBinary); |
| 86 | + this.OnOpen(); |
| 87 | + } |
| 88 | + |
| 89 | + void ws_Closed(object sender, EventArgs e) |
| 90 | + { |
| 91 | + var log = LogManager.GetLogger(Global.CallerName()); |
| 92 | + log.Info("ws_Closed"); |
| 93 | + ws.Opened -= ws_Opened; |
| 94 | + ws.Closed -= ws_Closed; |
| 95 | + ws.MessageReceived -= ws_MessageReceived; |
| 96 | + ws.DataReceived -= ws_DataReceived; |
| 97 | + ws.Error -= ws_Error; |
| 98 | + this.OnClose(); |
| 99 | + } |
| 100 | + |
| 101 | + void ws_MessageReceived(object sender, MessageReceivedEventArgs e) |
| 102 | + { |
| 103 | + var log = LogManager.GetLogger(Global.CallerName()); |
| 104 | + log.Info("ws_MessageReceived e.Message= " + e.Message); |
| 105 | + this.OnData(e.Message); |
| 106 | + } |
| 107 | + |
| 108 | + void ws_Error(object sender, SuperSocket.ClientEngine.ErrorEventArgs e) |
| 109 | + { |
| 110 | + this.OnError("websocket error", e.Exception); |
| 111 | + } |
| 112 | + |
| 113 | + protected override void Write(System.Collections.Immutable.ImmutableList<Parser.Packet> packets) |
| 114 | + { |
| 115 | + Writable = false; |
| 116 | + foreach (var packet in packets) |
| 117 | + { |
| 118 | + Parser.Parser.EncodePacket(packet, new WriteEncodeCallback(this)); |
| 119 | + } |
| 120 | + |
| 121 | + // fake drain |
| 122 | + // defer to next tick to allow Socket to clear writeBuffer |
| 123 | + //EasyTimer.SetTimeout(() => |
| 124 | + //{ |
| 125 | + Writable = true; |
| 126 | + Emit(EVENT_DRAIN); |
| 127 | + //}, 1); |
| 128 | + } |
| 129 | + |
| 130 | + public class WriteEncodeCallback : IEncodeCallback |
| 131 | + { |
| 132 | + private WebSocket webSocket; |
| 133 | + |
| 134 | + public WriteEncodeCallback(WebSocket webSocket) |
| 135 | + { |
| 136 | + this.webSocket = webSocket; |
| 137 | + } |
| 138 | + |
| 139 | + public void Call(object data) |
| 140 | + { |
| 141 | + //var log = LogManager.GetLogger(Global.CallerName()); |
| 142 | + |
| 143 | + if (data is string) |
| 144 | + { |
| 145 | + webSocket.ws.Send((string)data); |
| 146 | + } |
| 147 | + else if (data is byte[]) |
| 148 | + { |
| 149 | + var d = (byte[])data; |
| 150 | + |
| 151 | + //try |
| 152 | + //{ |
| 153 | + // var dataString = BitConverter.ToString(d); |
| 154 | + // //log.Info(string.Format("WriteEncodeCallback byte[] data {0}", dataString)); |
| 155 | + //} |
| 156 | + //catch (Exception e) |
| 157 | + //{ |
| 158 | + // log.Error(e); |
| 159 | + //} |
| 160 | + |
| 161 | + webSocket.ws.Send(d, 0, d.Length); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + protected override void DoClose() |
| 169 | + { |
| 170 | + if (ws != null) |
| 171 | + { |
| 172 | + |
| 173 | + try |
| 174 | + { |
| 175 | + ws.Close(); |
| 176 | + } |
| 177 | + catch (Exception e) |
| 178 | + { |
| 179 | + var log = LogManager.GetLogger(Global.CallerName()); |
| 180 | + log.Info("DoClose ws.Close() Exception= " + e.Message); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + |
| 186 | + |
| 187 | + public string Uri() |
| 188 | + { |
| 189 | + Dictionary<string, string> query = null; |
| 190 | + query = this.Query == null ? new Dictionary<string, string>() : new Dictionary<string, string>(this.Query); |
| 191 | + var schema = this.Secure ? "wss" : "ws"; |
| 192 | + var portString = ""; |
| 193 | + |
| 194 | + if (this.TimestampRequests) |
| 195 | + { |
| 196 | + query.Add(this.TimestampParam, DateTime.Now.Ticks.ToString() + "-" + Transport.Timestamps++); |
| 197 | + } |
| 198 | + |
| 199 | + var _query = ParseQS.Encode(query); |
| 200 | + |
| 201 | + if (this.Port > 0 && (("wss" == schema && this.Port != 443) |
| 202 | + || ("ws" == schema && this.Port != 80))) |
| 203 | + { |
| 204 | + portString = ":" + this.Port; |
| 205 | + } |
| 206 | + |
| 207 | + if (_query.Length > 0) |
| 208 | + { |
| 209 | + _query = "?" + _query; |
| 210 | + } |
| 211 | + |
| 212 | + return schema + "://" + this.Hostname + portString + this.Path + _query; |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments