示例#1
0
        public WebsocketServer(string ip, int port, string path)
        {
            Logger.Info("Starting server at http://{0}:{1}{2}...", ip, port, path);
            _server = new HttpServer(IPAddress.Parse(ip), port);
            var html = Html.Replace("{ws_url}", ip + path);

            _server.OnGet += (sender, e) => {
                var res  = e.Response;
                var data = Encoding.UTF8.GetBytes(html);
                res.StatusCode      = (int)HttpStatusCode.OK;
                res.ContentType     = "text/html";
                res.ContentEncoding = Encoding.UTF8;
                res.ContentLength64 = data.Length;
                res.OutputStream.Write(data, 0, data.Length);
            };
            _server.AddWebSocketService(path, () => {
                var socket = new DmdSocket(this);
                _sockets.Add(socket);
                return(socket);
            });
            _server.Start();
            if (_server.IsListening)
            {
                Logger.Info("Server listening, connect to ws://{0}:{1}{2}...", ip, port, path);
            }
        }
示例#2
0
 internal void Closed(DmdSocket socket)
 {
     _sockets.Remove(socket);
     Logger.Debug("WebSocket {0} closed", socket.ID);
 }