示例#1
0
        // Websocket server events
        private void _appServer_SessionClosed(WebSocketSession session, CloseReason reason)
        {
            // Stop keepalive message timer
            KeepAliveDMYMsgTimer keepaliveMsgTimer = (KeepAliveDMYMsgTimer)session.Items["KeepAliveTimer"];

            keepaliveMsgTimer.Stop();

            IPAddress destinationAddress = session.RemoteEndPoint.Address;

            logger.Info("Session closed from : " + destinationAddress.ToString() + " Reason :" + reason.ToString());
        }
        /// <summary>
        /// Initialize appserver with defining events.
        /// </summary>
        /// <returns>Initialized appserver.</returns>
        private WebSocketServer initializeAppServer()
        {
            WebSocketServer server = new WebSocketServer();

            server.NewSessionConnected += (session) =>
            {
                KeepAliveDMYMsgTimer keepAliveMsgTimer = new KeepAliveDMYMsgTimer(session, this.KeepAliveInterval);
                keepAliveMsgTimer.Start();
                session.Items.Add("KeepAliveTimer", keepAliveMsgTimer);

                IPAddress destinationAddress = session.RemoteEndPoint.Address;
                logger.Info("New session connected from : " + destinationAddress.ToString());
            };
            server.SessionClosed += (session, reason) =>
            {
                // Stop keepalive message timer
                KeepAliveDMYMsgTimer keepaliveMsgTimer = (KeepAliveDMYMsgTimer)session.Items["KeepAliveTimer"];
                keepaliveMsgTimer.Stop();

                IPAddress destinationAddress = session.RemoteEndPoint.Address;
                logger.Info("Session closed from : " + destinationAddress.ToString() + " Reason :" + reason.ToString());
            };
            server.NewMessageReceived += (session, message) =>
            {
                string received_JSON_mode;
                try
                {
                    var msg_dict = JsonConvert.DeserializeObject <Dictionary <string, string> >(message);
                    received_JSON_mode = msg_dict["mode"];
                }
                catch (KeyNotFoundException ex)
                {
                    error_msg(session, ex.GetType().ToString() + " " + ex.Message);
                    return;
                }
                catch (JsonException ex)
                {
                    error_msg(session, ex.GetType().ToString() + " " + ex.Message);
                    return;
                }

                try
                {
                    switch (received_JSON_mode)
                    {
                    //SSM COM all reset
                    case (ResetJSONFormat.ModeCode):
                        fuelTripCalc.resetSectTripFuel();
                        fuelTripCalc.resetTotalTripFuel();
                        response_msg(session, "NenpiCalc AllRESET.");
                        break;

                    case (SectResetJSONFormat.ModeCode):
                        fuelTripCalc.resetSectTripFuel();
                        response_msg(session, "NenpiCalc SectRESET.");
                        break;

                    case (SectSpanJSONFormat.ModeCode):
                        SectSpanJSONFormat span_jsonobj = JsonConvert.DeserializeObject <SectSpanJSONFormat>(message);
                        span_jsonobj.Validate();
                        fuelTripCalc.SectSpan = span_jsonobj.sect_span * 1000;
                        response_msg(session, "NenpiCalc SectSpan Set to : " + span_jsonobj.sect_span.ToString() + "sec");
                        break;

                    case (SectStoreMaxJSONFormat.ModeCode):
                        SectStoreMaxJSONFormat storemax_jsonobj = JsonConvert.DeserializeObject <SectStoreMaxJSONFormat>(message);
                        storemax_jsonobj.Validate();
                        fuelTripCalc.SectStoreMax = storemax_jsonobj.storemax;
                        response_msg(session, "NenpiCalc SectStoreMax Set to : " + storemax_jsonobj.storemax.ToString());
                        break;

                    default:
                        throw new JSONFormatsException("Unsuppoted mode property.");
                    }
                }
                catch (JSONFormatsException ex)
                {
                    error_msg(session, ex.GetType().ToString() + " " + ex.Message);
                    return;
                }
                catch (JsonException ex)
                {
                    error_msg(session, ex.GetType().ToString() + " " + ex.Message);
                    return;
                }
            };

            return(server);
        }