forked from kidgrow-microservices-platform

zhaoxiaohao
2021-04-26 9adebb75e04e8b0b369ebd318495cb683aa385b8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html  xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>服务端</title>
    <link rel="stylesheet" th:href="@{../layui/css/layui.css}" media="all" />
    <link rel="stylesheet" th:href="@{../layui/css/layui.mobile.css}" media="all" />
    <script th:src="@{../jquery.min.js}"></script>
    <script th:src="@{../layui/layui.all.js}"></script>
    <script th:src="@{../layui/layui.js}"></script>
    <style>
        .threed{
            text-align: center;
            color: #eb7350;
            -webkit-text-stroke: 1px black;
            letter-spacing: 0.04em;
            background-color: white;
        }
    </style>
</head>
<body>
 
 
<div class="layui-row">
    <h1 class="threed">WebSocket服务端</h1>
    <div class="layui-col-md8 layui-col-md-offset2" style="margin-top: 20px">
            <h1 style="margin-top: 100px">服务端</h1>
            <div class="layui-card">
                <div class="layui-card-body-inline" style="padding-bottom: 20px">
                    <div class="layui-form-item">
                    <label class="layui-form-label">在线数量:<span  id="sum" th:text="${num}">0</span></label>
                    </div>
 
                    <form class="layui-form" action="">
                        <div class="layui-form-item">
                            <label class="layui-form-label">在线人:</label>
                            <div class="layui-input-block">
                                <input type="checkbox" th:each="user: ${users}"  name="check" th:title="${user}">
                            </div>
                        </div>
                    </form>
 
                    <div class="layui-form-item">
                        <label class="layui-form-label">信息内容:</label>
                        <div class="layui-input-inline" style="width: 300px">
                            <input id="msg" type="text" name="title" required  lay-verify="required" placeholder="请输入要发送的内容" autocomplete="off" class="layui-input">
                        </div>
                    </div>
 
                    <button class="layui-btn" onclick="sendMsg()">发送</button>
                    <button class="layui-btn" onclick="sendAll()">全部发送</button>
                </div>
            </div>
            <div class="layui-card" style="margin-top: 100px">
                <div class="layui-card-header">
                    <h1>操作详情</h1>
                </div>
 
                <div id="content" class="layui-card-body" placeholder="请输入要发送的内容">
 
                </div>
            </div>
    </div>
</div>
   <script type="text/javascript">
 
       layui.use('form', function () {
           var form = layui.form;
       });
       function sendMsg(){
           var user = [];
           $("input[name='check']:checked").each(function(i){//把所有被选中的复选框的值存入数组
               user =user+$(this).attr("title")+","
           });
           console.log(user)
 
            if (user.length > 0) {
                user = user.substr(0, user.length - 1);
            } else {
               console.log("未选中发送人")
                var content = $("#content").html();
                $("#content").html(content+'<div style="margin-bottom: 8px">\n' +
                    '                        <p><q style="color: red">'+'系统提示:请在多选框中选择要发送的用户'+ '</span></p>\n' +
                    '                    </div>\n' +
                    '                    <br/>');
                return
            }
 
           var msg = $("#msg").val();
           if(msg!=null){
                $.ajax({
                    method: 'get',
                    url: '/sendmsg',
                    data: {
                        "username": "111",
                        "msg": msg
                    },
                    success:function(data) {
                        var content = $("#content").html();
                        $("#content").html(content+'<div style="margin-bottom: 8px">\n' +
                            '                        <p><q style="color: #eb7350">'+'服务器推送  '+msg+' -->'+user+ '</span></p>\n' +
                            '                    </div>\n' +
                            '                    <br/>');
                        console.log(data);
                    }
                })
           }else{
               alert("请填写要发送的用户昵称或者发送内容");
           }
       }
 
       function sendAll(){
           var msg = $("#msg").val();
           if(msg!=null){
               $.ajax({
                   method: 'get',
                   url: '/sendAll',
                   data:{
                       msg:msg
                   },
                   success:function(data) {
                       var content = $("#content").html();
                       $("#content").html(content+'<div style="margin-bottom: 8px">\n' +
                           '                        <p><q style="color: #eb7350">'+'服务器推送  '+msg+' --> 所有用户'+ '</span></p>\n' +
                           '                    </div>\n' +
                           '                    <br/>');
                       console.log(data);
                   }
               })
           }else{
               alert("请填写要发送的内容");
           }
       }
 
       function connect(){
               if ('WebSocket' in window){
                   //ws = new WebSocket("ws://www.isuyu.cn:8086/socketServer/niezhiliang9595");
                   ws = new WebSocket("ws://192.168.1.103:8088/socketServer/niezhiliang9595");
               }
               else if ('MozWebSocket' in window){
                   //ws = new MozWebSocket("ws://www.isuyu.cn:8086/socketServer/niezhiliang9595");
                   ws = new MozWebSocket("ws://192.168.1.103:8088/socketServer/niezhiliang9595");
               }
               else{
                   alert("该浏览器不支持websocket");
               }
               ws.onmessage = function(evt) {
                   var content = $("#content").html();
                   $("#content").html(content+'<div style="text-align: right;margin-bottom: 8px">\n' +
                       '                        <p><q style="color: mediumorchid;text-align: right">'+evt.data+ '</span></p>\n' +
                       '                    </div>\n' +
                       '                    <br/>');
                   console.log(msg)
               };
 
               ws.onclose = function(evt) {
                  console.log('连接关闭')
               };
 
               ws.onopen = function(evt) {
                   var content = $("#content").html();
                   $("#content").html(content+'<div style="margin-bottom: 8px">\n' +
                       '                        <p><q style="color: #eb7350">'+'服务器初始化成功...'+ '</span></p>\n' +
                       '                    </div>\n' +
                       '                    <br/>');
                  console.log('连接成功')
               };
       }
           connect()
   </script>
</body>
 
</html>