首页 前端知识 websocket 接收信息 nodeJs

websocket 接收信息 nodeJs

2024-11-10 09:11:16 前端知识 前端哥 686 105 我要收藏

Node .js

var ws = require("nodejs-websocket");
console.log("开始建立连接...")


var server = ws.createServer(function(conn) {
    conn.on("text", function(str) {
        console.log("message:" + str)


        setInterval(() => {
            function selectrandfrom(lowValue, highValue) { //随机数
                var choice = highValue - lowValue + 1;
                return Math.floor(Math.random() * choice + lowValue);
            }
            // mock数据
            const getRandom = (n = 8000) => parseInt(Math.random() * n + 1)

            let dataArr = [...Array(12)].map((v, index) => ({
                name: `api名称`,
                num: getRandom()
            }))
            let dataArray = {
                    dataArr,
                    testData: [selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100)],
                    dataNum: [{
                        name: '受理',
                        data: [selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100)]
                    }, {
                        name: '办结',
                        data: [selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100)]
                    }, {
                        name: '即将逾期',
                        data: [selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100)]
                    }, {
                        name: '逾期',
                        data: [selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100)]
                    }, ],
                    name: [selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100), selectrandfrom(0, 100)],
                    score: [selectrandfrom(0, 10), selectrandfrom(0, 10), selectrandfrom(0, 10), selectrandfrom(0, 10), selectrandfrom(0, 10), selectrandfrom(0, 10)]
                }
                // 定时向客户端发送数据
            conn.send(JSON.stringify(dataArray));
        }, 1000 * 5)

    })
    conn.on("close", function(code, reason) {
        console.log("关闭连接")
    });
    conn.on("error", function(code, reason) {
        console.log("异常关闭", code, reason)
    });
}).listen(8001)
console.log("WebSocket建立完毕")

//客户端页面展示:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/echarts@4.3.0/dist/echarts.min.js"></script>
</head>

<body>
    <!--  ECharts 准备一个定义了宽高的 DOM -->
    <div>
        <div id="main" style="width: 100%;height:100vh;margin:0 auto;"></div>
        <div id="main2" style="width: 100%;height:100vh;margin:0 auto;"></div>
        <div id="main3" style="width: 100%;height:100vh;margin:0 auto;"></div>
    </div>
    <script type="text/javascript">
        let dataArray = {};
        let dataArr = null;
        if (window.WebSocket) {
            var ws = new WebSocket('ws://localhost:8001');

            ws.onopen = function(e) {
                console.log("连接服务器成功");
                // 向服务器发送消息
                ws.send("what`s your name?");
            }
            ws.onclose = function(e) {
                console.log("服务器关闭");
            }
            ws.onerror = function() {
                    console.log("连接出错");
                }
                // 接收服务器的消息
            ws.onmessage = function(e) {

                dataArray = JSON.parse(e.data);
                console.log(dataArray);
                initEchart();
                initEchart3(dataArray.testData);
                dataArr = dataArray.dataArr;
                initEchart2()
            }
        }

        function initEchart() {
            // 基于准备好的dom,初始化echarts实例
            var myChart = echarts.init(document.getElementById('main'));



            let links = dataArray.score.map((item, i) => ({
                source: i,
                target: i + 1
            }));

            let color = [
                [{
                    offset: 0,
                    color: '#dff7ff'
                }, {
                    offset: 0.07,
                    color: '#4ad2ff'
                }, {
                    offset: 1,
                    color: 'rgba(74, 210, 255, .1)'
                }],
                [{
                    offset: 0,
                    color: '#ffffff'
                }, {
                    offset: 0.07,
                    color: '#29ff60'
                }, {
                    offset: 1,
                    color: 'rgba(41, 255, 96, .1)'
                }],
                [{
                    offset: 0,
                    color: '#fffdef'
                }, {
                    offset: 0.07,
                    color: '#ffd83e'
                }, {
                    offset: 1,
                    color: 'rgba(255, 216, 62, .1)'
                }],
                [{
                    offset: 0,
                    color: '#ffeaea'
                }, {
                    offset: 0.07,
                    color: '#ff5676'
                }, {
                    offset: 1,
                    color: 'rgba(255, 86, 118, .1)'
                }],
            ]
            let color2 = ['#4ad9ff', '#4ad2ff', '#29ff60', '#ffd83e', '#ff5676']

            // series
            let series = dataArray.dataNum.map((v, i) => ({
                name: v.name,
                type: 'pictorialBar',
                symbol: 'path://M32.000,0.000 L64.000,800.000 L0.000,800.000 L32.000,0.000 Z',
                // 是否裁剪图形
                symbolClip: false,
                barWidth: 10,
                // 间隔
                barGap: '10%',
                data: v.data,
                itemStyle: {
                    color: params => {
                        return new echarts.graphic.LinearGradient(0, 0, 0, 1, color[i])
                    }
                }
            }))

            // 插入关系图配置
            series.splice(0, 0, {
                // 第二坐标轴,即右边
                yAxisIndex: 1,
                name: '满意度评分',
                type: 'graph',
                layout: 'none',
                coordinateSystem: 'cartesian2d',
                symbolSize: 50,
                // 文字显示
                label: {
                    show: true,
                    formatter: params => {
                        return params.value
                    },
                    textStyle: {
                        align: 'center',
                        fontSize: 14,
                        color: '#fff'
                    }
                },
                // 关系轴样式
                edgeSymbol: ['circle', 'arrow'],
                edgeSymbolSize: [4, 10],
                data: dataArray.score,
                links: links,
                lineStyle: {
                    color: color[0]
                }
            })

            let getLegend = dataArray.dataNum.map((v, i) => ({
                    name: v.name,
                    textStyle: {
                        fontSize: 14,
                        color: '#fff'
                    }
                }))
                // legend
            let legend = [{
                name: '满意度评分',
                data: ['满意度评分'],
                textStyle: {
                    fontSize: 14,
                    color: '#fff'
                },
                right: 340,
                top: 6,
            }, {
                data: getLegend,
                right: 20,
                top: 6,
                itemGap: 15
            }]


            // tooltip
            let tooltip = {
                trigger: 'axis',
                textStyle: {
                    fontSize: 18
                },
                axisPointer: {
                    type: 'cross',
                    label: {
                        backgroundColor: '#283b56'
                    }
                },
                formatter: v => {
                    let [a, b, c, d, e] = v
                    return `
    <div class='u-p-2'>
        <div>满意度评分:${a.value}</div>
        <div class='u-mt-2'>受理:${b.value}</div>
        <div class='u-mt-2'>办结:${c.value}</div>
        <div class='u-mt-2'>即将逾期:${d.value}</div>
        <div class='u-mt-2'>逾期:${e.value}</div>
    </div>
`
                }
            }

            // grid
            let grid = {
                top: '15%',
                left: '10%',
                right: '5%',
                bottom: '24%'
            }

            // xAxis
            let xAxis = {
                type: 'category',
                boundaryGap: true,
                data: dataArray.name,
                axisLine: {
                    lineStyle: {
                        color: 'rgba(0,129,251,1)'
                    }
                },
                axisLabel: {
                    textStyle: {
                        fontSize: 16,
                        color: '#bdcbfd'
                    },
                    rotate: 0
                },
                axisTick: {
                    show: false
                }, //坐标轴刻度
            }

            // yAxis
            let yAxis = [{
                name: '工单量(件)',
                type: 'value',
                scale: true,
                min: 0,
                nameTextStyle: {
                    color: '#81b1ff',
                    fontSize: 12,
                    padding: [0, 0, 0, 80]
                },
                boundaryGap: [0.2, 0.2],
                splitLine: {
                    show: false
                },
                axisLine: {
                    lineStyle: {
                        color: 'rgba(0,129,251,1)'
                    }
                },
                axisLabel: {
                    textStyle: {
                        fontSize: 16,
                        color: '#bdcbfd'
                    }
                }
            }, {
                name: '评分',
                // 隐藏第二坐标轴
                show: false
            }]

            // 渲染
            option = {
                    tooltip,
                    grid,
                    xAxis,
                    yAxis,
                    series,
                    legend,
                    color: color2,
                    backgroundColor: 'rgba(0,0,0,1)'
                }
                // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);
        }



        function initEchart2() {
            // 基于准备好的dom,初始化echarts实例
            var myChart2 = echarts.init(document.getElementById('main2'));

            // 排下序就好看很多
            const dataArray = dataArr.sort((a, b) => a.num - b.num)

            const xdataName = dataArray.map(v => v.name),
                dataNum = dataArray.map(v => v.num)

            // tooltip
            const tooltip = {
                trigger: 'axis',
                textStyle: {
                    fontSize: '100%'
                },
                formatter: v => {
                    return `
			<div class='u-p-2'>
				<div>${v[0].name}:${v[0].data}</div>
			</div>
		`
                }
            }

            // grid
            const grid = {
                top: '5%',
                left: '16%',
                right: '18%',
                bottom: '5%'
            }

            // xAxis
            const xAxis = {
                splitLine: {
                    show: false
                },
                axisLine: {
                    show: false
                },
                axisLabel: {
                    show: false
                },
                axisTick: {
                    show: false
                }
            }

            // yAxis
            const yAxis = [{
                type: "category",
                inverse: false,
                data: xdataName,
                axisLabel: {
                    formatter: (params, index) => {
                        // 因为是倒序,所以得把「索引」翻转一下
                        return `${dataArray.length - index}  ${params}`
                    },
                },
                // 把坐标轴、刻度、坐标线统统不要
                axisLine: {
                    show: false
                },
                axisTick: {
                    show: false
                },
                splitLine: {
                    show: false
                },
            }, {
                type: 'category',
                data: dataNum,
                axisLabel: {
                    margin: 40,
                    formatter: (params, index) => {
                        return `{a${dataArray.length - index < 3 ? dataArray.length - index : ''}|${params} }{b${dataArray.length - index < 3 ? dataArray.length - index : ''}|}`
                    },

                    rich: {
                        a: {
                            fontSize: 24,
                            color: '#a7b9ff',
                            verticalAlign: 'bottom'
                        },
                        a1: {
                            fontSize: 24,
                            color: '#7dfff9',
                            verticalAlign: 'bottom'
                        },
                        a2: {
                            fontSize: 24,
                            color: '#60e3ff',
                            verticalAlign: 'bottom'
                        },
                        a3: {
                            fontSize: 24,
                            color: '#71d2ff',
                            verticalAlign: 'bottom'
                        },

                        b: {
                            fontSize: 12,
                            color: '#a7b9ff',
                            verticalAlign: 'bottom'
                        },
                        b1: {
                            fontSize: 12,
                            color: '#7dfff9',
                            verticalAlign: 'bottom'
                        },
                        b2: {
                            fontSize: 12,
                            color: '#60e3ff',
                            verticalAlign: 'bottom'
                        },
                        b3: {
                            fontSize: 12,
                            color: '#71d2ff',
                            verticalAlign: 'bottom'
                        },
                    }
                },
                // 把坐标轴、刻度、坐标线统统不要
                axisLine: {
                    show: false
                },
                axisTick: {
                    show: false
                },
                splitLine: {
                    show: false
                },
            }]

            // series
            const series = [{
                z: 6,
                type: "pictorialBar",
                symbol: 'path://M107.000,71.000 C104.936,71.000 102.665,70.806 100.273,70.467 C94.592,76.922 86.275,81.000 77.000,81.000 C70.794,81.000 65.020,79.170 60.172,76.029 C66.952,74.165 72.647,69.714 76.173,63.817 C69.821,61.362 64.063,58.593 60.000,56.039 L60.000,52.813 C70.456,53.950 80.723,55.000 83.000,55.000 C88.972,55.000 93.000,53.723 93.000,50.000 C93.000,47.071 89.222,45.000 83.000,45.000 C80.723,45.000 70.456,46.050 60.000,47.187 L60.000,43.989 C64.057,41.431 69.807,38.644 76.168,36.173 C72.641,30.281 66.948,25.834 60.172,23.971 C65.020,20.830 70.794,19.000 77.000,19.000 C86.270,19.000 94.584,23.074 100.265,29.524 C102.647,29.191 104.918,29.000 107.000,29.000 C129.644,29.000 148.000,50.000 148.000,50.000 C148.000,50.000 129.644,71.000 107.000,71.000 ZM113.000,38.000 C106.373,38.000 101.000,43.373 101.000,50.000 C101.000,56.627 106.373,62.000 113.000,62.000 C119.627,62.000 125.000,56.627 125.000,50.000 C125.000,43.373 119.627,38.000 113.000,38.000 ZM113.000,56.000 C109.686,56.000 107.000,53.314 107.000,50.000 C107.000,46.686 109.686,44.000 113.000,44.000 C116.314,44.000 119.000,46.686 119.000,50.000 C119.000,53.314 116.314,56.000 113.000,56.000 ZM110.500,19.000 C109.567,19.000 108.763,18.483 108.334,17.726 C100.231,9.857 89.187,5.000 77.000,5.000 C64.813,5.000 53.769,9.857 45.666,17.726 C45.237,18.483 44.433,19.000 43.500,19.000 C42.119,19.000 41.000,17.881 41.000,16.500 C41.000,15.847 41.256,15.259 41.665,14.813 L41.575,14.718 C50.629,5.628 63.156,-0.000 77.000,-0.000 C90.844,-0.000 103.371,5.628 112.425,14.718 L112.335,14.813 C112.744,15.259 113.000,15.847 113.000,16.500 C113.000,17.881 111.881,19.000 110.500,19.000 ZM53.000,49.484 C61.406,48.626 77.810,47.000 81.345,47.000 C87.353,47.000 91.000,48.243 91.000,50.000 C91.000,52.234 87.111,53.000 81.345,53.000 C77.810,53.000 61.406,51.374 53.000,50.516 L53.000,49.484 ZM53.000,47.000 L9.000,50.000 L53.000,53.000 L53.000,56.000 L-0.000,50.000 L53.000,44.000 L53.000,47.000 ZM43.500,81.000 C44.433,81.000 45.237,81.517 45.666,82.274 C53.769,90.143 64.813,95.000 77.000,95.000 C89.187,95.000 100.231,90.143 108.334,82.274 C108.763,81.517 109.567,81.000 110.500,81.000 C111.881,81.000 113.000,82.119 113.000,83.500 C113.000,84.153 112.744,84.741 112.335,85.187 L112.425,85.282 C103.371,94.372 90.844,100.000 77.000,100.000 C63.156,100.000 50.629,94.372 41.575,85.282 L41.665,85.187 C41.256,84.741 41.000,84.153 41.000,83.500 C41.000,82.119 42.119,81.000 43.500,81.000 Z', // 「小飞机」
                symbolPosition: 'end', // 在数据结尾显示
                symbolSize: [40, 30], // 「飞机大小」
                symbolOffset: [40, 0], //「偏移量」
                itemStyle: {
                    color: {
                        type: 'linear',
                        x: 1,
                        y: 0,
                        x2: 0,
                        y2: 0,
                        colorStops: [{
                            offset: 0,
                            color: 'rgba(180, 255, 252, 1)' //  0%  处的颜色
                        }, {
                            offset: .8,
                            color: 'rgba(35,147,252,0.4)' //  100%  处的颜色
                        }, {
                            offset: 1,
                            color: 'rgba(89,124,255,0.1)' //  100%  处的颜色
                        }],
                        global: false //  缺省为  false
                    }
                },
                data: dataNum
            }, {
                z: 6,
                type: "pictorialBar",
                symbolClip: false,
                // 三角柱状矢量图
                symbol: 'path://M0.000,160.000 L0.000,0.000 C0.000,0.000 238.197,29.897 520.969,44.235 C957.229,66.354 1512.000,80.000 1512.000,80.000 C1512.000,80.000 958.247,93.978 521.021,115.845 C235.724,130.113 0.000,160.000 0.000,160.000 Z',
                itemStyle: {
                    color: {
                        type: 'linear',
                        x: 1,
                        y: 0,
                        x2: 0,
                        y2: 0,
                        colorStops: [{
                            offset: 0,
                            color: 'rgba(180, 255, 252, 1)' //  0%  处的颜色
                        }, {
                            offset: .8,
                            color: 'rgba(35,147,252,0.4)' //  100%  处的颜色
                        }, {
                            offset: 1,
                            color: 'rgba(89,124,255,0.1)' //  100%  处的颜色
                        }],
                        global: false //  缺省为  false
                    }
                },
                data: dataNum
            }]
            option = {
                    tooltip,
                    grid,
                    xAxis,
                    yAxis,
                    series,
                    backgroundColor: 'rgba(0,0,0,1)'
                }
                // 使用刚指定的配置项和数据显示图表。
            myChart2.setOption(option);
        }

        function initEchart3(data) {
            // 基于准备好的dom,初始化echarts实例
            var myChart3 = echarts.init(document.getElementById('main3'));
            option = {
                backgroundColor: '#041F4A',
                xAxis: {
                    type: 'category',
                    data: ['银宝', '个险', '团险', '银宝', '个险', '团险', '银宝', '个险', '团险', '银宝', '个险', '团险'],
                    axisLine: {
                        show: true,
                        onZero: true,
                        symbol: "none",
                        lineStyle: {
                            color: "#e5e5e5"
                        }
                    },
                    axisTick: {
                        show: false
                    },
                },
                yAxis: {
                    show: false,
                    type: 'value',
                    axisTick: {
                        show: false
                    },
                    axisLine: {
                        show: false
                    },
                    axisLabel: {
                        show: false
                    }
                },
                //图表与容器的位置关系
                grid: {
                    left: '3%', // 与容器左侧的距离
                    right: '3%', // 与容器右侧的距离
                    top: '11%', // 与容器顶部的距离
                    bottom: '12%', // 与容器底部的距离
                },
                series: [{
                    data,
                    type: 'bar',
                    backgroundStyle: {
                        color: "rgba(111, 111, 22, 1)"
                    },
                    //坐标轴显示器的文本标签
                    label: {
                        show: true,
                        position: 'top',
                        color: '#e5e5e5',
                        zlevel: 4,
                        z: 10,
                    },
                    barWidth: 30,
                    //柱条颜色
                    itemStyle: {
                        color: {
                            type: 'linear',
                            x: 0,
                            y: 0,
                            x2: 0,
                            y2: 1,
                            colorStops: [{
                                offset: 0,
                                color: 'rgba(242,7,7,0.9)' // 0% 处的颜色
                            }, {
                                offset: .8,
                                color: 'rgba(7,242,128,0.3)' // 100% 处的颜色
                            }, {
                                offset: 1,
                                color: 'rgba(242,7,228,0.3)' // 100% 处的颜色
                            }],
                            global: false // 缺省为 false
                        }
                    },
                    animationEasing: "linear",
                    animationEasingUpdate: "quadraticIn", //数据更新时的缓动效果
                    animationDurationUpdate: 300, //数据更新动画的时长
                    animation: true, //开启动画
                    animationDuration: 0,
                    z: 1,
                }, {
                    // 值分隔
                    type: "pictorialBar",
                    itemStyle: {
                        normal: {
                            color: "#0F375F",
                        },
                    },
                    symbolRepeat: "fixed",
                    symbolMargin: 3,
                    symbol: "rect",
                    symbolClip: true,
                    symbolSize: [30, 3],
                    symbolPosition: "start",
                    symbolOffset: [0, -1],
                    // symbolBoundingData: this.total,
                    data: [52, 60, 45, 38, 30, 51, 12, 20, 15, 62, 60, 45],
                    width: 25,
                    z: 0,
                    zlevel: 3,
                }, {
                    //辅助背景图形
                    name: "背景条",
                    type: "bar", //pictorialBar
                    barWidth: "30",
                    barGap: "-100%",
                    itemStyle: {
                        normal: {
                            borderWidth: 0,
                            color: "rgba(146,158,48,0.2)",
                        },
                        barBorderRadius: 10,
                    },
                    data: [62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62],
                    z: 0,
                    zlevel: 0,
                }, {
                    // 背景分隔
                    type: "pictorialBar",
                    itemStyle: {
                        normal: {
                            color: "#0F375F",
                        },
                    },
                    symbolRepeat: "fixed",
                    symbolMargin: 3,
                    symbol: "rect",
                    symbolClip: true,
                    symbolSize: [30, 3],
                    symbolPosition: "start",
                    symbolOffset: [0, -1],
                    // symbolBoundingData: this.total,
                    data: [60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60],
                    width: 25,
                    z: 0,
                    zlevel: 1,
                }, ]
            };
            myChart3.setOption(option);
        }
    </script>
</body>

</html>

效果图:

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

转载请注明出处或者链接地址:https://www.qianduange.cn//article/20130.html
标签
评论
发布的文章
大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!