chore: 调整接口访问权限
parent
ff8f12ddc0
commit
04e8229f89
|
@ -33,6 +33,11 @@ namespace LeanCloud.Realtime.Internal.Connection {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private const int HEART_BEAT_INTERVAL = 30000;
|
private const int HEART_BEAT_INTERVAL = 30000;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 子协议
|
||||||
|
/// </summary>
|
||||||
|
private const string SUB_PROTOCOL = "lc.protobuf2.3";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 通知事件
|
/// 通知事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -79,11 +84,11 @@ namespace LeanCloud.Realtime.Internal.Connection {
|
||||||
LCRTMServer rtmServer = await router.GetServer();
|
LCRTMServer rtmServer = await router.GetServer();
|
||||||
try {
|
try {
|
||||||
LCLogger.Debug($"Primary Server");
|
LCLogger.Debug($"Primary Server");
|
||||||
await client.Connect(rtmServer.Primary);
|
await client.Connect(rtmServer.Primary, SUB_PROTOCOL);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LCLogger.Error(e);
|
LCLogger.Error(e);
|
||||||
LCLogger.Debug($"Secondary Server");
|
LCLogger.Debug($"Secondary Server");
|
||||||
await client.Connect(rtmServer.Secondary);
|
await client.Connect(rtmServer.Secondary, SUB_PROTOCOL);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw e;
|
throw e;
|
||||||
|
|
|
@ -5,7 +5,6 @@ using System.Collections.ObjectModel;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using LeanCloud.Realtime.Internal.Protocol;
|
using LeanCloud.Realtime.Internal.Protocol;
|
||||||
using LeanCloud.Storage.Internal;
|
|
||||||
using LeanCloud.Storage.Internal.Codec;
|
using LeanCloud.Storage.Internal.Codec;
|
||||||
using LeanCloud.Common;
|
using LeanCloud.Common;
|
||||||
|
|
||||||
|
@ -441,7 +440,8 @@ namespace LeanCloud.Realtime.Internal.Controller {
|
||||||
command.ConvMessage = convMessage;
|
command.ConvMessage = convMessage;
|
||||||
GenericCommand response = await Connection.SendRequest(command);
|
GenericCommand response = await Connection.SendRequest(command);
|
||||||
JsonObjectMessage results = response.ConvMessage.Results;
|
JsonObjectMessage results = response.ConvMessage.Results;
|
||||||
List<object> convs = JsonConvert.DeserializeObject<List<object>>(results.Data, new LCJsonConverter());
|
List<object> convs = JsonConvert.DeserializeObject<List<object>>(results.Data,
|
||||||
|
LCJsonConverter.Default);
|
||||||
return convs.Select(item => {
|
return convs.Select(item => {
|
||||||
Dictionary<string, object> conv = item as Dictionary<string, object>;
|
Dictionary<string, object> conv = item as Dictionary<string, object>;
|
||||||
string convId = conv["objectId"] as string;
|
string convId = conv["objectId"] as string;
|
||||||
|
@ -478,7 +478,8 @@ namespace LeanCloud.Realtime.Internal.Controller {
|
||||||
request.ConvMessage = convMessage;
|
request.ConvMessage = convMessage;
|
||||||
GenericCommand response = await Connection.SendRequest(request);
|
GenericCommand response = await Connection.SendRequest(request);
|
||||||
JsonObjectMessage results = response.ConvMessage.Results;
|
JsonObjectMessage results = response.ConvMessage.Results;
|
||||||
List<object> convs = JsonConvert.DeserializeObject<List<object>>(results.Data, new LCJsonConverter());
|
List<object> convs = JsonConvert.DeserializeObject<List<object>>(results.Data,
|
||||||
|
LCJsonConverter.Default);
|
||||||
List<LCIMTemporaryConversation> convList = convs.Select(item => {
|
List<LCIMTemporaryConversation> convList = convs.Select(item => {
|
||||||
LCIMTemporaryConversation temporaryConversation = new LCIMTemporaryConversation(Client);
|
LCIMTemporaryConversation temporaryConversation = new LCIMTemporaryConversation(Client);
|
||||||
temporaryConversation.MergeFrom(item as Dictionary<string, object>);
|
temporaryConversation.MergeFrom(item as Dictionary<string, object>);
|
||||||
|
@ -800,7 +801,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
|
||||||
private async Task OnPropertiesUpdated(ConvCommand conv) {
|
private async Task OnPropertiesUpdated(ConvCommand conv) {
|
||||||
LCIMConversation conversation = await Client.GetOrQueryConversation(conv.Cid);
|
LCIMConversation conversation = await Client.GetOrQueryConversation(conv.Cid);
|
||||||
Dictionary<string, object> updatedAttr = JsonConvert.DeserializeObject<Dictionary<string, object>>(conv.AttrModified.Data,
|
Dictionary<string, object> updatedAttr = JsonConvert.DeserializeObject<Dictionary<string, object>>(conv.AttrModified.Data,
|
||||||
new LCJsonConverter());
|
LCJsonConverter.Default);
|
||||||
// 更新内存数据
|
// 更新内存数据
|
||||||
conversation.MergeInfo(updatedAttr);
|
conversation.MergeInfo(updatedAttr);
|
||||||
Client.OnConversationInfoUpdated?.Invoke(conversation,
|
Client.OnConversationInfoUpdated?.Invoke(conversation,
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using LeanCloud.Storage.Internal;
|
|
||||||
using LeanCloud.Common;
|
using LeanCloud.Common;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
@ -9,7 +8,7 @@ namespace LeanCloud.Realtime.Internal.Router {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// RTM Router
|
/// RTM Router
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class LCRTMRouter {
|
public class LCRTMRouter {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 请求超时
|
/// 请求超时
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -17,14 +16,14 @@ namespace LeanCloud.Realtime.Internal.Router {
|
||||||
|
|
||||||
private LCRTMServer rtmServer;
|
private LCRTMServer rtmServer;
|
||||||
|
|
||||||
internal LCRTMRouter() {
|
public LCRTMRouter() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取服务器地址
|
/// 获取服务器地址
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
internal async Task<LCRTMServer> GetServer() {
|
public async Task<LCRTMServer> GetServer() {
|
||||||
if (rtmServer == null || !rtmServer.IsValid) {
|
if (rtmServer == null || !rtmServer.IsValid) {
|
||||||
await Fetch();
|
await Fetch();
|
||||||
}
|
}
|
||||||
|
@ -53,7 +52,7 @@ namespace LeanCloud.Realtime.Internal.Router {
|
||||||
response.Dispose();
|
response.Dispose();
|
||||||
LCHttpUtils.PrintResponse(response, resultString);
|
LCHttpUtils.PrintResponse(response, resultString);
|
||||||
|
|
||||||
rtmServer = JsonConvert.DeserializeObject<LCRTMServer>(resultString, new LCJsonConverter());
|
rtmServer = JsonConvert.DeserializeObject<LCRTMServer>(resultString, LCJsonConverter.Default);
|
||||||
|
|
||||||
return rtmServer;
|
return rtmServer;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,38 +2,38 @@
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace LeanCloud.Realtime.Internal.Router {
|
namespace LeanCloud.Realtime.Internal.Router {
|
||||||
internal class LCRTMServer {
|
public class LCRTMServer {
|
||||||
[JsonProperty("groupId")]
|
[JsonProperty("groupId")]
|
||||||
internal string GroupId {
|
public string GroupId {
|
||||||
get; set;
|
get; set;
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonProperty("groupUrl")]
|
[JsonProperty("groupUrl")]
|
||||||
internal string GroupUrl {
|
public string GroupUrl {
|
||||||
get; set;
|
get; set;
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonProperty("server")]
|
[JsonProperty("server")]
|
||||||
internal string Primary {
|
public string Primary {
|
||||||
get; set;
|
get; set;
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonProperty("secondary")]
|
[JsonProperty("secondary")]
|
||||||
internal string Secondary {
|
public string Secondary {
|
||||||
get; set;
|
get; set;
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonProperty("ttl")]
|
[JsonProperty("ttl")]
|
||||||
internal int Ttl {
|
public int Ttl {
|
||||||
get; set;
|
get; set;
|
||||||
}
|
}
|
||||||
|
|
||||||
DateTimeOffset createdAt;
|
DateTimeOffset createdAt;
|
||||||
|
|
||||||
internal LCRTMServer() {
|
public LCRTMServer() {
|
||||||
createdAt = DateTimeOffset.Now;
|
createdAt = DateTimeOffset.Now;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool IsValid => DateTimeOffset.Now < createdAt + TimeSpan.FromSeconds(Ttl);
|
public bool IsValid => DateTimeOffset.Now < createdAt + TimeSpan.FromSeconds(Ttl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Net.WebSockets;
|
using System.Net.WebSockets;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace LeanCloud.Realtime.Internal.WebSocket {
|
namespace LeanCloud.Realtime.Internal.WebSocket {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// WebSocket 客户端,负责底层连接和事件,只与通信协议相关
|
/// WebSocket 客户端,负责底层连接和事件,只与通信协议相关
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class LCWebSocketClient {
|
public class LCWebSocketClient {
|
||||||
// .net standard 2.0 好像在拼合 Frame 时有 bug,所以将这个值调整大一些
|
// .net standard 2.0 好像在拼合 Frame 时有 bug,所以将这个值调整大一些
|
||||||
private const int RECV_BUFFER_SIZE = 1024 * 5;
|
private const int RECV_BUFFER_SIZE = 1024 * 5;
|
||||||
|
|
||||||
|
@ -23,12 +24,12 @@ namespace LeanCloud.Realtime.Internal.WebSocket {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 消息事件
|
/// 消息事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal Action<byte[]> OnMessage;
|
public Action<byte[]> OnMessage;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 连接关闭
|
/// 连接关闭
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal Action OnClose;
|
public Action OnClose;
|
||||||
|
|
||||||
private ClientWebSocket ws;
|
private ClientWebSocket ws;
|
||||||
|
|
||||||
|
@ -37,11 +38,14 @@ namespace LeanCloud.Realtime.Internal.WebSocket {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="server"></param>
|
/// <param name="server"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
internal async Task Connect(string server) {
|
public async Task Connect(string server,
|
||||||
|
string subProtocol = null) {
|
||||||
LCLogger.Debug($"Connecting WebSocket: {server}");
|
LCLogger.Debug($"Connecting WebSocket: {server}");
|
||||||
Task timeoutTask = Task.Delay(CONNECT_TIMEOUT);
|
Task timeoutTask = Task.Delay(CONNECT_TIMEOUT);
|
||||||
ws = new ClientWebSocket();
|
ws = new ClientWebSocket();
|
||||||
ws.Options.AddSubProtocol("lc.protobuf2.3");
|
if (!string.IsNullOrEmpty(subProtocol)) {
|
||||||
|
ws.Options.AddSubProtocol(subProtocol);
|
||||||
|
}
|
||||||
Task connectTask = ws.ConnectAsync(new Uri(server), default);
|
Task connectTask = ws.ConnectAsync(new Uri(server), default);
|
||||||
if (await Task.WhenAny(connectTask, timeoutTask) == connectTask) {
|
if (await Task.WhenAny(connectTask, timeoutTask) == connectTask) {
|
||||||
LCLogger.Debug($"Connected WebSocket: {server}");
|
LCLogger.Debug($"Connected WebSocket: {server}");
|
||||||
|
@ -57,7 +61,7 @@ namespace LeanCloud.Realtime.Internal.WebSocket {
|
||||||
/// 主动关闭连接
|
/// 主动关闭连接
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
internal async Task Close() {
|
public async Task Close() {
|
||||||
LCLogger.Debug("Closing WebSocket");
|
LCLogger.Debug("Closing WebSocket");
|
||||||
OnMessage = null;
|
OnMessage = null;
|
||||||
OnClose = null;
|
OnClose = null;
|
||||||
|
@ -81,11 +85,12 @@ namespace LeanCloud.Realtime.Internal.WebSocket {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data"></param>
|
/// <param name="data"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
internal async Task Send(byte[] data) {
|
public async Task Send(byte[] data,
|
||||||
|
WebSocketMessageType messageType = WebSocketMessageType.Binary) {
|
||||||
ArraySegment<byte> bytes = new ArraySegment<byte>(data);
|
ArraySegment<byte> bytes = new ArraySegment<byte>(data);
|
||||||
if (ws.State == WebSocketState.Open) {
|
if (ws.State == WebSocketState.Open) {
|
||||||
try {
|
try {
|
||||||
await ws.SendAsync(bytes, WebSocketMessageType.Binary, true, default);
|
await ws.SendAsync(bytes, messageType, true, default);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LCLogger.Error(e);
|
LCLogger.Error(e);
|
||||||
throw e;
|
throw e;
|
||||||
|
@ -97,6 +102,15 @@ namespace LeanCloud.Realtime.Internal.WebSocket {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送文本数据
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task Send(string text) {
|
||||||
|
await Send(Encoding.UTF8.GetBytes(text), WebSocketMessageType.Text);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 接收数据
|
/// 接收数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -119,14 +133,12 @@ namespace LeanCloud.Realtime.Internal.WebSocket {
|
||||||
HandleExceptionClose();
|
HandleExceptionClose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (result.MessageType == WebSocketMessageType.Binary) {
|
} else {
|
||||||
// 拼合 WebSocket Message
|
// 拼合 WebSocket Message
|
||||||
int length = result.Count;
|
int length = result.Count;
|
||||||
byte[] data = new byte[length];
|
byte[] data = new byte[length];
|
||||||
Array.Copy(buffer, data, length);
|
Array.Copy(buffer, data, length);
|
||||||
OnMessage?.Invoke(data);
|
OnMessage?.Invoke(data);
|
||||||
} else {
|
|
||||||
LCLogger.Error($"Error message type: {result.MessageType}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using LeanCloud.Storage.Internal.Codec;
|
using LeanCloud.Storage.Internal.Codec;
|
||||||
using LeanCloud.Storage.Internal;
|
using LeanCloud.Common;
|
||||||
|
|
||||||
namespace LeanCloud.Realtime {
|
namespace LeanCloud.Realtime {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -115,7 +115,7 @@ namespace LeanCloud.Realtime {
|
||||||
|
|
||||||
internal static LCIMTypedMessage Deserialize(string json) {
|
internal static LCIMTypedMessage Deserialize(string json) {
|
||||||
Dictionary<string, object> msgData = JsonConvert.DeserializeObject<Dictionary<string, object>>(json,
|
Dictionary<string, object> msgData = JsonConvert.DeserializeObject<Dictionary<string, object>>(json,
|
||||||
new LCJsonConverter());
|
LCJsonConverter.Default);
|
||||||
LCIMTypedMessage message = null;
|
LCIMTypedMessage message = null;
|
||||||
int msgType = (int)msgData[MessageTypeKey];
|
int msgType = (int)msgData[MessageTypeKey];
|
||||||
if (customMessageDict.TryGetValue(msgType, out Func<LCIMTypedMessage> msgConstructor)) {
|
if (customMessageDict.TryGetValue(msgType, out Func<LCIMTypedMessage> msgConstructor)) {
|
||||||
|
|
Loading…
Reference in New Issue