Skip to content
This repository has been archived by the owner on Feb 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #169 from onerain88/fix_reconnect
Browse files Browse the repository at this point in the history
重构断线重连模块
  • Loading branch information
oneRain committed May 14, 2019
2 parents 2e955ee + 7f782a2 commit b1b0cde
Show file tree
Hide file tree
Showing 18 changed files with 602 additions and 358 deletions.
9 changes: 3 additions & 6 deletions LeanCloud.Realtime/Internal/Router/AVRouterController.cs
Expand Up @@ -123,12 +123,9 @@ Task<PushRouterState> QueryAsync(string pushRouter, bool secure, CancellationTok
{
if (t.Exception != null)
{
var innnerException = t.Exception.InnerException.InnerException.InnerException;
if (innnerException != null)
{
AVRealtime.PrintLog(innnerException.Message);
return null;
}
var innnerException = t.Exception.InnerException;
AVRealtime.PrintLog(innnerException.Message);
throw innnerException;
}
var httpStatus = (int)t.Result.Item1;
if (httpStatus != 200)
Expand Down
2 changes: 2 additions & 0 deletions LeanCloud.Realtime/Internal/WebSocket/IWebSocketClient.cs
Expand Up @@ -59,5 +59,7 @@ public interface IWebSocketClient
/// </summary>
/// <param name="message"></param>
void Send(string message);

Task<bool> Connect(string url, string protocol = null);
}
}
Expand Up @@ -40,11 +40,15 @@ public void Close()
{
if (connection != null)
{
connection.Close();
connection.OnOpened -= Connection_OnOpened;
connection.OnMessage -= Connection_OnMessage;
connection.OnClosed -= Connection_OnClosed;
connection.OnError -= Connection_OnError;
try {
connection.Close();
} catch (Exception e) {
AVRealtime.PrintLog(string.Format("close websocket error: {0}", e.Message));
}
}
}

Expand All @@ -69,27 +73,25 @@ public void Open(string url, string protocol = null)

private void Connection_OnOpened()
{
if (this.OnOpened != null)
this.OnOpened();
OnOpened?.Invoke();
}

private void Connection_OnMessage(string obj)
{
if (this.OnMessage != null)
this.OnMessage(obj);
AVRealtime.PrintLog("websocket<=" + obj);
OnMessage?.Invoke(obj);
}

private void Connection_OnClosed()
{
AVRealtime.PrintLog("PCL websocket closed without parameters.");
if (this.OnClosed != null)
this.OnClosed(0, "", "");
AVRealtime.PrintLog("PCL websocket closed without parameters..");
OnClosed?.Invoke(0, "", "");
}

private void Connection_OnError(string obj)
{
AVRealtime.PrintLog($"PCL websocket error: {obj}");
OnError?.Invoke(obj);
connection?.Close();
}

public void Send(string message)
Expand All @@ -104,12 +106,61 @@ public void Send(string message)
{
var log = "Connection is NOT open when send message";
AVRealtime.PrintLog(log);
Connection_OnError(log);
connection?.Close();
}
}
else {
AVRealtime.PrintLog("Connection is NULL");
}
}

public Task<bool> Connect(string url, string protocol = null) {
var tcs = new TaskCompletionSource<bool>();
Action onOpen = null;
Action onClose = null;
Action<string> onError = null;
onOpen = () => {
AVRealtime.PrintLog("PCL websocket opened");
connection.OnOpened -= onOpen;
connection.OnClosed -= onClose;
connection.OnError -= onError;
// 注册事件
connection.OnMessage += Connection_OnMessage;
connection.OnClosed += Connection_OnClosed;
connection.OnError += Connection_OnError;
tcs.SetResult(true);
};
onClose = () => {
connection.OnOpened -= onOpen;
connection.OnClosed -= onClose;
connection.OnError -= onError;
tcs.SetException(new Exception("连接关闭"));
};
onError = (err) => {
AVRealtime.PrintLog(string.Format("连接错误:{0}", err));
connection.OnOpened -= onOpen;
connection.OnClosed -= onClose;
connection.OnError -= onError;
try {
connection.Close();
} catch (Exception e) {
AVRealtime.PrintLog(string.Format("关闭错误的 WebSocket 异常:{0}", e.Message));
} finally {
tcs.SetException(new Exception(string.Format("连接错误:{0}", err)));
}
};

// 在每次打开时,重新创建 WebSocket 对象
connection = WebSocketFactory.Create();
connection.OnOpened += onOpen;
connection.OnClosed += onClose;
connection.OnError += onError;
//
if (!string.IsNullOrEmpty(protocol)) {
url = string.Format("{0}?subprotocol={1}", url, protocol);
}
connection.Open(url, protocol);
return tcs.Task;
}
}
}
Expand Up @@ -3,8 +3,7 @@
using System.Linq;
using System.Text;
using WebSocketSharp;
using UnityEngine;

using System.Threading.Tasks;

namespace LeanCloud.Realtime.Internal
{
Expand Down Expand Up @@ -73,23 +72,24 @@ public void Open(string url, string protocol = null)
ws.ConnectAsync();
}

private void OnClose(object sender, CloseEventArgs e)
private void OnWebSokectMessage(object sender, MessageEventArgs e)
{
AVRealtime.PrintLog("Unity websocket closed without parameters.");
if (this.OnClosed != null)
this.OnClosed(e.Code, e.Reason, "");
AVRealtime.PrintLog("websocket<=" + e.Data);
m_OnMessage?.Invoke(e.Data);
}

private void OnWebSokectMessage(object sender, MessageEventArgs e)
{
if (this.m_OnMessage != null)
this.m_OnMessage(e.Data);
private void OnClose(object sender, CloseEventArgs e) {
AVRealtime.PrintLog(string.Format("Unity websocket closed with {0}, {1}", e.Code, e.Reason));
OnClosed?.Invoke(e.Code, e.Reason, null);
}

private void OnOpen(object sender, EventArgs e)
{
if (this.OnOpened != null)
this.OnOpened();
void OnWebSocketError(object sender, ErrorEventArgs e) {
AVRealtime.PrintLog($"PCL websocket error: {e.Message}");
ws?.Close();
}

private void OnOpen(object sender, EventArgs e) {
OnOpened?.Invoke();
}

public void Send(string message)
Expand All @@ -99,5 +99,53 @@ public void Send(string message)
});
}

public Task<bool> Connect(string url, string protocol = null) {
var tcs = new TaskCompletionSource<bool>();
EventHandler onOpen = null;
EventHandler<CloseEventArgs> onClose = null;
EventHandler<ErrorEventArgs> onError = null;
onOpen = (sender, e) => {
AVRealtime.PrintLog("PCL websocket opened");
ws.OnOpen -= onOpen;
ws.OnClose -= onClose;
ws.OnError -= onError;
// 注册事件
ws.OnMessage += OnWebSokectMessage;
ws.OnClose += OnClose;
ws.OnError += OnWebSocketError;
tcs.SetResult(true);
};
onClose = (sender, e) => {
ws.OnOpen -= onOpen;
ws.OnClose -= onClose;
ws.OnError -= onError;
tcs.SetException(new Exception("连接关闭"));
};
onError = (sender, e) => {
AVRealtime.PrintLog(string.Format("连接错误:{0}", e.Message));
ws.OnOpen -= onOpen;
ws.OnClose -= onClose;
ws.OnError -= onError;
try {
ws.Close();
} catch (Exception ex) {
AVRealtime.PrintLog(string.Format("关闭错误的 WebSocket 异常:{0}", ex.Message));
} finally {
tcs.SetException(new Exception(string.Format("连接错误:{0}", e.Message)));
}
};

// 在每次打开时,重新创建 WebSocket 对象
if (!string.IsNullOrEmpty(protocol)) {
url = string.Format("{0}?subprotocol={1}", url, protocol);
}
ws = new WebSocket(url);
ws.OnOpen += onOpen;
ws.OnClose += onClose;
ws.OnError += onError;
ws.ConnectAsync();
return tcs.Task;
}
}
}

0 comments on commit b1b0cde

Please sign in to comment.