Beispiel #1
0
        private async Task OnProcess(ResponseBodyBuilder response, CancellationToken token)
        {
            using (var socket = new WebSocketHost(response.GetStream()))
                using (var watch = new ServerSessionWatch()) {
                    socket.Send("open");

                    var wRef = new WeakReference <WebSocketHost>(socket);

                    watch.SessionChanged += (o, e) => {
                        if (wRef.TryGetTarget(out var _socket))
                        {
                            try {
                                _socket.Send("message", e.Data);
                            }
                            catch (Exception error) {
                                Log.Warn("Failed to send WebSocket message!", error);
                            }
                        }
                    };

                    watch.Initialize();


                    // TODO: GET RID OF THIS!!!
                    // This is an awful hack, and never stops running!
                    // Need to read request and close both when closed.
                    while (!token.IsCancellationRequested)
                    {
                        await Task.Delay(200, token);
                    }
                }
        }
    }