chore: 调整接口访问权限

oneRain 2020-05-13 16:28:55 +08:00
parent ff8f12ddc0
commit 04e8229f89
6 changed files with 49 additions and 32 deletions

View File

@ -33,6 +33,11 @@ namespace LeanCloud.Realtime.Internal.Connection {
/// </summary>
private const int HEART_BEAT_INTERVAL = 30000;
/// <summary>
/// 子协议
/// </summary>
private const string SUB_PROTOCOL = "lc.protobuf2.3";
/// <summary>
/// 通知事件
/// </summary>
@ -79,11 +84,11 @@ namespace LeanCloud.Realtime.Internal.Connection {
LCRTMServer rtmServer = await router.GetServer();
try {
LCLogger.Debug($"Primary Server");
await client.Connect(rtmServer.Primary);
await client.Connect(rtmServer.Primary, SUB_PROTOCOL);
} catch (Exception e) {
LCLogger.Error(e);
LCLogger.Debug($"Secondary Server");
await client.Connect(rtmServer.Secondary);
await client.Connect(rtmServer.Secondary, SUB_PROTOCOL);
}
} catch (Exception e) {
throw e;

View File

@ -5,7 +5,6 @@ using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Newtonsoft.Json;
using LeanCloud.Realtime.Internal.Protocol;
using LeanCloud.Storage.Internal;
using LeanCloud.Storage.Internal.Codec;
using LeanCloud.Common;
@ -441,7 +440,8 @@ namespace LeanCloud.Realtime.Internal.Controller {
command.ConvMessage = convMessage;
GenericCommand response = await Connection.SendRequest(command);
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 => {
Dictionary<string, object> conv = item as Dictionary<string, object>;
string convId = conv["objectId"] as string;
@ -478,7 +478,8 @@ namespace LeanCloud.Realtime.Internal.Controller {
request.ConvMessage = convMessage;
GenericCommand response = await Connection.SendRequest(request);
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 => {
LCIMTemporaryConversation temporaryConversation = new LCIMTemporaryConversation(Client);
temporaryConversation.MergeFrom(item as Dictionary<string, object>);
@ -800,7 +801,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
private async Task OnPropertiesUpdated(ConvCommand conv) {
LCIMConversation conversation = await Client.GetOrQueryConversation(conv.Cid);
Dictionary<string, object> updatedAttr = JsonConvert.DeserializeObject<Dictionary<string, object>>(conv.AttrModified.Data,
new LCJsonConverter());
LCJsonConverter.Default);
// 更新内存数据
conversation.MergeInfo(updatedAttr);
Client.OnConversationInfoUpdated?.Invoke(conversation,

View File

@ -1,7 +1,6 @@
using System;
using System.Threading.Tasks;
using System.Net.Http;
using LeanCloud.Storage.Internal;
using LeanCloud.Common;
using Newtonsoft.Json;
@ -9,7 +8,7 @@ namespace LeanCloud.Realtime.Internal.Router {
/// <summary>
/// RTM Router
/// </summary>
internal class LCRTMRouter {
public class LCRTMRouter {
/// <summary>
/// 请求超时
/// </summary>
@ -17,14 +16,14 @@ namespace LeanCloud.Realtime.Internal.Router {
private LCRTMServer rtmServer;
internal LCRTMRouter() {
public LCRTMRouter() {
}
/// <summary>
/// 获取服务器地址
/// </summary>
/// <returns></returns>
internal async Task<LCRTMServer> GetServer() {
public async Task<LCRTMServer> GetServer() {
if (rtmServer == null || !rtmServer.IsValid) {
await Fetch();
}
@ -53,7 +52,7 @@ namespace LeanCloud.Realtime.Internal.Router {
response.Dispose();
LCHttpUtils.PrintResponse(response, resultString);
rtmServer = JsonConvert.DeserializeObject<LCRTMServer>(resultString, new LCJsonConverter());
rtmServer = JsonConvert.DeserializeObject<LCRTMServer>(resultString, LCJsonConverter.Default);
return rtmServer;
}

View File

@ -2,38 +2,38 @@
using Newtonsoft.Json;
namespace LeanCloud.Realtime.Internal.Router {
internal class LCRTMServer {
public class LCRTMServer {
[JsonProperty("groupId")]
internal string GroupId {
public string GroupId {
get; set;
}
[JsonProperty("groupUrl")]
internal string GroupUrl {
public string GroupUrl {
get; set;
}
[JsonProperty("server")]
internal string Primary {
public string Primary {
get; set;
}
[JsonProperty("secondary")]
internal string Secondary {
public string Secondary {
get; set;
}
[JsonProperty("ttl")]
internal int Ttl {
public int Ttl {
get; set;
}
DateTimeOffset createdAt;
internal LCRTMServer() {
public LCRTMServer() {
createdAt = DateTimeOffset.Now;
}
internal bool IsValid => DateTimeOffset.Now < createdAt + TimeSpan.FromSeconds(Ttl);
public bool IsValid => DateTimeOffset.Now < createdAt + TimeSpan.FromSeconds(Ttl);
}
}

View File

@ -1,12 +1,13 @@
using System;
using System.Threading.Tasks;
using System.Net.WebSockets;
using System.Text;
namespace LeanCloud.Realtime.Internal.WebSocket {
/// <summary>
/// WebSocket 客户端,负责底层连接和事件,只与通信协议相关
/// </summary>
internal class LCWebSocketClient {
public class LCWebSocketClient {
// .net standard 2.0 好像在拼合 Frame 时有 bug所以将这个值调整大一些
private const int RECV_BUFFER_SIZE = 1024 * 5;
@ -23,12 +24,12 @@ namespace LeanCloud.Realtime.Internal.WebSocket {
/// <summary>
/// 消息事件
/// </summary>
internal Action<byte[]> OnMessage;
public Action<byte[]> OnMessage;
/// <summary>
/// 连接关闭
/// </summary>
internal Action OnClose;
public Action OnClose;
private ClientWebSocket ws;
@ -37,11 +38,14 @@ namespace LeanCloud.Realtime.Internal.WebSocket {
/// </summary>
/// <param name="server"></param>
/// <returns></returns>
internal async Task Connect(string server) {
public async Task Connect(string server,
string subProtocol = null) {
LCLogger.Debug($"Connecting WebSocket: {server}");
Task timeoutTask = Task.Delay(CONNECT_TIMEOUT);
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);
if (await Task.WhenAny(connectTask, timeoutTask) == connectTask) {
LCLogger.Debug($"Connected WebSocket: {server}");
@ -57,7 +61,7 @@ namespace LeanCloud.Realtime.Internal.WebSocket {
/// 主动关闭连接
/// </summary>
/// <returns></returns>
internal async Task Close() {
public async Task Close() {
LCLogger.Debug("Closing WebSocket");
OnMessage = null;
OnClose = null;
@ -81,11 +85,12 @@ namespace LeanCloud.Realtime.Internal.WebSocket {
/// </summary>
/// <param name="data"></param>
/// <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);
if (ws.State == WebSocketState.Open) {
try {
await ws.SendAsync(bytes, WebSocketMessageType.Binary, true, default);
await ws.SendAsync(bytes, messageType, true, default);
} catch (Exception e) {
LCLogger.Error(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>
@ -119,14 +133,12 @@ namespace LeanCloud.Realtime.Internal.WebSocket {
HandleExceptionClose();
}
}
} else if (result.MessageType == WebSocketMessageType.Binary) {
} else {
// 拼合 WebSocket Message
int length = result.Count;
byte[] data = new byte[length];
Array.Copy(buffer, data, length);
OnMessage?.Invoke(data);
} else {
LCLogger.Error($"Error message type: {result.MessageType}");
}
}
} catch (Exception e) {

View File

@ -2,7 +2,7 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using LeanCloud.Storage.Internal.Codec;
using LeanCloud.Storage.Internal;
using LeanCloud.Common;
namespace LeanCloud.Realtime {
/// <summary>
@ -115,7 +115,7 @@ namespace LeanCloud.Realtime {
internal static LCIMTypedMessage Deserialize(string json) {
Dictionary<string, object> msgData = JsonConvert.DeserializeObject<Dictionary<string, object>>(json,
new LCJsonConverter());
LCJsonConverter.Default);
LCIMTypedMessage message = null;
int msgType = (int)msgData[MessageTypeKey];
if (customMessageDict.TryGetValue(msgType, out Func<LCIMTypedMessage> msgConstructor)) {