[Idea] Move Receive() to it's own Listen() method
まだ誰も着手していません。
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 38/100
- issue の種類
- 機能追加
- 明瞭さ
- 明確に書かれている
- 活発さ
- 停滞
- 技術スタック
- csharp, unity
- 領域
- networking
調査の方向性
まず Websocket.cs とその WebGL 実装を読みます。Receive() の呼び出しを Connect() から分離し、issue に記載された Listen() メソッドと WebGL 互換メソッドを追加してから、呼び出し側が await Connect() を実行し、特別な WebGL 処理なしに open ハンドラーからリッスンを開始できることを確認します。
索引モデルが 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 にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
endel/NativeWebSocket のほかの issue
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
endel/NativeWebSocket#107 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
endel/NativeWebSocket#102 · コメント 1 件 · リアクション 1 件 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 25/100
endel/NativeWebSocket#101 · コメント 6 件 · リアクション 2 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 20/100
endel/NativeWebSocket#95 · リアクション 1 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 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 ·