Bladeren bron

增加websocket服务示例

xuwei 2 jaren geleden
bovenliggende
commit
90adc1e824

BIN
wwwroot/Bin/Fleck.dll


BIN
wwwroot/Img/bg9in1.png


+ 90 - 0
wwwroot/main/manage/demo.html

@@ -0,0 +1,90 @@
+<!doctype html>
+<html lang="en" data-bs-theme="auto">
+<head>
+    <meta charset="utf-8">
+    <meta name="viewport" content="width=device-width">
+    <link rel="icon" href="/app/images/dongke-logo.png">
+    <link href="/plugins/bootstrap/bootstrap.min.css" rel="stylesheet">
+    <title>东科软件</title>
+    <style>
+        body {
+            background-color:black;
+        }
+        .col {
+            width:1280px;
+            height:720px;
+        }
+        .boardone {
+            background-repeat:no-repeat;
+            background-position:center;
+            background-image:url(/Img/bg9in1.png);
+        }
+    </style>
+</head>
+<body class="m-0 p-0 border-0">
+
+    <div v-scope="main()" @vue:mounted="init" class="m-1">
+        <div class="row row-cols-3 text-center g-2">
+            <div class="col mx-1 boardone">
+                1号看板
+            </div>
+            <div class="col mx-1 boardone">
+                2号看板
+            </div>
+            <div class="col mx-1 boardone">
+                3号看板
+            </div>
+            <div class="col mx-1 boardone">
+                4号看板
+            </div>
+            <div class="col mx-1 boardone">
+                5号看板
+            </div>
+            <div class="col mx-1 boardone">
+                6号看板
+            </div>
+            <div class="col mx-1 boardone">
+                7号看板
+            </div>
+            <div class="col mx-1 boardone">
+                8号看板
+            </div>
+            <div class="col mx-1 boardone">
+                9号看板
+            </div>
+        </div>
+    </div>
+
+    <script src="/plugins/bootstrap/bootstrap.bundle.min.js"></script>
+    <script src="/plugins/vue/petite-vue.iife.js"></script>
+    <script src="/plugins/axios/axios.min.js"></script>
+
+    <script>
+        function main() {
+            return {
+                //设备看板列表
+                data: [],
+                //初始化
+                init() {
+
+                },
+                //加载数据
+                load() {
+                    let self = this;
+
+                    //axios.get('/app/api/getDashboardList.ashx')
+                    //    .then(function (response) {
+                    //        self.dashboardList = response.data;
+                    //    })
+                    //    .catch(function (error) {
+
+                    //    });
+
+                }
+            }
+        }
+        PetiteVue.createApp().mount();
+    </script>
+
+</body>
+</html>

+ 58 - 0
wwwroot/main/websocket/index.ashx

@@ -0,0 +1,58 @@
+<%@ WebHandler Language="C#" Class="index" %>
+
+using System;
+using System.Web;
+using System.Collections.Generic;
+using Fleck;
+using System.Linq;
+
+/// <summary>
+/// 测试WebSocket服务
+/// xuwei add 2024-03-26
+/// </summary>
+public class index : IHttpHandler
+{
+    public static List<IWebSocketConnection> allScokets;
+    public static WebSocketServer server;
+
+    public void ProcessRequest(HttpContext context)
+    {
+        //启动服务
+        if (context.Request["start"] is object)
+        {
+            start();
+            context.Response.Write("服务启动成功!");
+        }
+
+        //发送数据
+        if (context.Request["barcode"] is object)
+        {
+            foreach (var socket in allScokets.ToList())
+            {
+                socket.Send("条码:" + context.Request["barcode"].ToString());
+            }
+        }
+    }
+
+    //启动服务
+    public static void start()
+    {
+        allScokets = new List<IWebSocketConnection>();
+        server = new WebSocketServer("ws://127.0.0.1:9200");
+        server.Start(scoket =>
+        {
+            scoket.OnOpen = () => { allScokets.Add(scoket); };
+            scoket.OnClose = () => { allScokets.Remove(scoket); };
+            scoket.OnMessage = message => { allScokets.ToList().ForEach(s => s.Send(message)); };
+        });
+    }
+
+    public bool IsReusable
+    {
+        get
+        {
+            return false;
+        }
+    }
+
+}

+ 33 - 0
wwwroot/main/websocket/index.html

@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>WebSocket Example</title>
+</head>
+<body>
+    <h1>WebSocket示例</h1>
+    <div id="messages"></div>
+
+    <script>
+
+        const socket = new WebSocket('ws://127.0.0.1:9200');
+
+        socket.onopen = function(event) {
+            console.log('WebSocket 已连接!等待接收数据');
+        };
+
+        socket.onmessage = function(event) {
+            const messages = document.getElementById('messages');
+            messages.innerHTML += '<p>接收数据:' + event.data + '</p>';
+            console.log(event.data);
+        };
+
+        socket.onerror = function(error) {
+            console.error('WebSocket 错误: ' + error);
+        };
+
+        socket.onclose = function(event) {
+            console.log('WebSocket 已闭');
+        };
+    </script>
+</body>
+</html>