[Idea] Move Receive() to it's own Listen() method
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 38/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Ferma
- Stack tecnologico
- csharp, unity
- Ambito
- networking
Direzione di ricerca
Leggi prima Websocket.cs e la relativa implementazione WebGL. Separa la chiamata Receive() da Connect(), aggiungi il metodo Listen() e il metodo di compatibilità WebGL descritto nell'issue, quindi verifica che i chiamanti possano eseguire await Connect() e iniziare l'ascolto dall'handler open senza una gestione speciale di WebGL.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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
- Lingua principale
- C#
- Stelle
- 1.7k
- Fork
- 206
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di endel/NativeWebSocket
-
Open UPM compatibility Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
endel/NativeWebSocket#107 ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
endel/NativeWebSocket#102 · 1 commento · 1 reazione ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 25/100
endel/NativeWebSocket#101 · 6 commenti · 2 reazioni ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 20/100
endel/NativeWebSocket#95 · 1 reazione ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 20/100
endel/NativeWebSocket#94 · 2 commenti ·
Tutte le issue di endel/NativeWebSocket
Issue simili
-
type/automation type/tech-debt
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
-
t/bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
-
ci-failure-cause test-failure
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
-
area:auth FE mvp P3
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
klasolsson81/jobbliggaren#1788 ·