Пример #1
0
        // buffer: buffer for recv
        private WebSocket(ByteBuffer buffer, string url, List <string> protocols)
        {
            _url       = url;
            _buffer    = buffer;
            _protocols = protocols != null?protocols.ToArray() : new string[]
            {
                ""
            };

            _rwlock.EnterWriteLock();
            _websockets.Add(this);
            _rwlock.ExitWriteLock();

            do
            {
                if (_protocols != null && _protocols.Length > 0)
                {
                    _context = WSApi.ulws_create(_protocols[0], _callback, 1024 * 4, 1024 * 4);
                    if (_context.IsValid())
                    {
                        SetReadyState(ReadyState._CONSTRUCTED);
                        break;
                    }
                }
                SetReadyState(ReadyState.CLOSED);
            } while (false);
        }
Пример #2
0
        // _duk_lws_destroy
        private void Destroy()
        {
            if (_is_context_destroyed)
            {
                return;
            }

            if (_is_polling)
            {
                _is_context_destroying = true;
                return;
            }

            SetReadyState(ReadyState.CLOSED);
            _is_context_destroyed = true;
            if (_context.IsValid())
            {
                // WSApi.ulws_destroy(_context);
                _context = lws_context.Null;
            }

            while (_pending.Count > 0)
            {
                var packet = _pending.Dequeue();
                packet.Release();
            }
            _bufferedAmount = 0;

            if (_buffer != null)
            {
                _buffer.Release();
                _buffer = null;
            }
            var runtime = ScriptEngine.GetRuntime(_jsContext);

            if (runtime != null)
            {
                runtime.OnUpdate  -= Update;
                runtime.OnDestroy -= Destroy;
            }

            _jsContext = JSContext.Null;
            _jsThis    = JSApi.JS_UNDEFINED;
            _rwlock.EnterWriteLock();
            _websockets.Remove(this);
            _rwlock.ExitWriteLock();
        }