Browse Source

增加日志,半夜常连接偶尔会掉?

qinqi 1 year ago
parent
commit
329e50c547
2 changed files with 36 additions and 17 deletions
  1. 35 16
      wwwroot/main/Assemble/api/GetBarcodeByScan.ashx
  2. 1 1
      wwwroot/main/Assemble/index.html

+ 35 - 16
wwwroot/main/Assemble/api/GetBarcodeByScan.ashx

@@ -37,12 +37,24 @@ public class GetBarcodeByScan : IHttpHandler, IReadOnlySessionState
         if (context.Request["start"] is object)
         {
             string barcodeip = context.Request["barcodeip"].ToString();
+            string name = context.Request["name"].ToString();
 
-            if (allScokets == null)
+            if (allScokets == null )
             {
                 start(barcodeip);
+                context.Response.Write("服务启动成功!");
+                Logger.Info("服务启动成功;工位:" + name + "; Scokets:" + allScokets.Count);
             }
-            context.Response.Write("服务启动成功!");
+            else
+            {
+                context.Response.Write("已启动");
+                foreach (var socket in allScokets.ToList())
+                {
+                    Logger.Info("已启动;工位:"  + name + "; Scokets:" +socket);
+                }
+                Logger.Info("已启动;工位:"  + name + "; Scokets:" +allScokets.Count);
+            }
+            
         }
 
         if (context.Request["barcode"] is object)
@@ -80,7 +92,7 @@ public class GetBarcodeByScan : IHttpHandler, IReadOnlySessionState
                 var scanflag = Convert.ToInt16(context.Request["scanFlag"]);
                 var plcip = context.Request["plcip"].ToString();
                 var db = "1000";
-                if (context.Request["db"] is object) { 
+                if (context.Request["db"] is object) {
                     db = context.Request["db"].ToString();
                 }
                 PLC.dbbarResult = db+".0";
@@ -119,8 +131,8 @@ public class GetBarcodeByScan : IHttpHandler, IReadOnlySessionState
 
                 var scanflag = Convert.ToInt16(context.Request["scanFlag"]);
                 var plcip = context.Request["plcip"].ToString();
-               var db = "1000";
-                if (context.Request["db"] is object) { 
+                var db = "1000";
+                if (context.Request["db"] is object) {
                     db = context.Request["db"].ToString();
                 }
                 PLC.dbbarResult = db+".0";
@@ -151,13 +163,13 @@ public class GetBarcodeByScan : IHttpHandler, IReadOnlySessionState
             try
             {
                 var scanBomFlag = Convert.ToInt16(context.Request["scanBomFlag"]);
-                if (context.Request["plcip"] is object) { 
+                if (context.Request["plcip"] is object) {
                     var plcip = context.Request["plcip"].ToString();
                     var db = "1000";
-                    if (context.Request["db"] is object) { 
+                    if (context.Request["db"] is object) {
                         db = context.Request["db"].ToString();
                     }
-                 
+
                     PLC.dbidnrkResult = db+".2";
                     s7 = new S7(plcip);
                     //s7.Open(plcip, 102);
@@ -190,7 +202,7 @@ public class GetBarcodeByScan : IHttpHandler, IReadOnlySessionState
                     }
                 }
                 //var plcip = context.Request["plcip"].ToString();
-                
+
 
             }
             catch (Exception ex)
@@ -204,14 +216,21 @@ public class GetBarcodeByScan : IHttpHandler, IReadOnlySessionState
     //启动服务
     public static void start(string ip)
     {
-        allScokets = new List<IWebSocketConnection>();
-        server = new WebSocketServer(ip);
-        server.Start(scoket =>
+        try
+        {
+            allScokets = new List<IWebSocketConnection>();
+            server = new WebSocketServer(ip);
+            server.Start(scoket =>
+            {
+                scoket.OnOpen = () => { allScokets.Add(scoket); };
+                scoket.OnClose = () => { allScokets.Remove(scoket); };
+                scoket.OnMessage = message => { allScokets.ToList().ForEach(s => s.Send(message)); };
+            });
+                }
+        catch (Exception ex)
         {
-            scoket.OnOpen = () => { allScokets.Add(scoket); };
-            scoket.OnClose = () => { allScokets.Remove(scoket); };
-            scoket.OnMessage = message => { allScokets.ToList().ForEach(s => s.Send(message)); };
-        });
+            throw ex;
+        }
     }
 
     public bool IsReusable

+ 1 - 1
wwwroot/main/Assemble/index.html

@@ -911,7 +911,7 @@
             //console.log('socket', socket)
             //console.log('socket1', socket1)
 
-            var result = $.ajax({ url: 'api/GetBarcodeByScan.ashx?start=1&barcodeip=ws://172.18.32.32:9102', dataType: "json", type: "get" });
+            var result = $.ajax({ url: 'api/GetBarcodeByScan.ashx?start=1&barcodeip=ws://172.18.32.32:9102&name=' + request('NAME').toString(), dataType: "json", type: "get" });
             console.log('socketbarcode', socketbarcode)
 
             loadProductionPlanData();