[Idea] Move Receive() to it's own Listen() method
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 38/100
- Issue 类型
- 功能
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
- 技术栈
- csharp, unity
- 领域
- networking
调研方向
首先阅读 Websocket.cs 及其 WebGL 实现。将 Receive() 调用与 Connect() 分离,添加 issue 中描述的 Listen() 方法和 WebGL 兼容方法,然后验证调用方可以 await Connect(),并从 open handler 开始监听,而无需进行特殊的 WebGL 处理。
由索引模型根据 Issue 内容生成。
描述
Current Receive() is called at the end of Connect(). This means that if you ever want to await the Connect() call (so that you move on when it's connected), it won't actually return until the socket is disconnected. Moving it to it's own Listen() method would allow for that.
Something like:
// Websocket.cs
// 1) Remove the "await Receive()" call from the end of Connect()
// 2) Add an empty Listen() method in the WebGL implementation for compatibility
// 3) Add the following method:
public async void Listen()
{
try
{
await Receive();
}
catch (Exception ex)
{
OnError?.Invoke(ex.Message);
OnClose?.Invoke(WebSocketCloseCode.Abnormal);
}
finally
{
if (m_Socket != null)
{
m_TokenSource.Cancel();
m_Socket.Dispose();
}
}
}
This lets you handle the websocket connection like so:
// WebsocketConnection.cs
public async Task ConnectAsync( string uri )
{
if( this.isOpen )
{
Debug.LogWarning( "[WebSocketConnection] Trying to connect a websocket, but we're already open" );
return; // we're already connected
}
// start our connection
this.m_uri = uri;
this.m_socket = new WebSocket( this.m_uri );
// add our events
this.m_socket.OnOpen += this._onWebSocketOpen;
this.m_socket.OnClose += this._onWebSocketClose;
this.m_socket.OnError += this._onWebSocketError;
this.m_socket.OnMessage += this._onWebSocketMessage;
Trace.Log( $"[WebSocketConnection] Opening a new connection to {this.m_uri}" );
await this.m_socket.Connect();
// wait until the socket is actually open
await new WaitUntil( () => this.isOpen );
}
public void Update()
{
#if !UNITY_WEBGL || UNITY_EDITOR
if( this.isOpen )
this.m_socket.DispatchMessageQueue();
#endif
}
// called when our socket is open and ready for business
private void _onWebSocketOpen()
{
Debug.Log( $"[WebSocketConnection] Connection to {this.m_uri} open!" );
// start listening on the socket (NOTE: we're not awaiting here)
this.m_socket.Listen();
}
Then actually using it in a method becomes much simpler. You can connect with something like:
public async void JoinGame()
{
// check if we need to connect
if( !this.m_websocketConnection.isConnected )
await this.m_websocketConnection.ConnectAsync( this.m_websocketServerURI );
this.m_websocketConnection.JoinGame();
}
As an added benefit, you don't need special handling for WebGL and nothing blocks unless it should
- 主要语言
- C#
- 星标
- 1.7k
- 派生
- 206
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
endel/NativeWebSocket 的其他 Issue
-
难度 4/5 3-5 天 新手友好度 48/100
endel/NativeWebSocket#107 ·
-
难度 4/5 3-5 天 新手友好度 35/100
endel/NativeWebSocket#102 · 1 条评论 · 1 个 reaction ·
-
难度 3/5 1-2 天 新手友好度 25/100
endel/NativeWebSocket#101 · 6 条评论 · 2 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 20/100
endel/NativeWebSocket#95 · 1 个 reaction ·
-
难度 5/5 一周以上 新手友好度 20/100
endel/NativeWebSocket#94 · 2 条评论 ·
查看 endel/NativeWebSocket 的全部 Issue
相似的 Issue
-
type/automation type/tech-debt
难度 2/5 1-3 小时 新手友好度 78/100
-
bug
难度 2/5 1-3 小时 新手友好度 88/100
-
t/bug
难度 2/5 1-3 小时 新手友好度 82/100
-
ci-failure-cause test-failure
难度 2/5 1-3 小时 新手友好度 82/100
-
area:auth FE mvp P3
难度 2/5 1-3 小时 新手友好度 88/100
klasolsson81/jobbliggaren#1788 ·