xiaoyong931011
2021-04-12 a13a93a493e7e94e28b2225c26e7e13b52d3288c
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
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <!-- 启用360浏览器的极速模式(webkit) -->
    <meta name="renderer" content="webkit">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <title>soketTest</title>
    <link th:href="@{/images/}"
          rel="SHORTCUT ICON">
    <meta name="keywords"
          content=""/>
    <meta name="description"
          content=""/>
    <style type="text/css">
        .row > div:nth-child(1) {
            background: url(../../images/login-title.jpg) no-repeat;
            background-size: 100% 100%;
            height: 380px;
        }
 
        .row > div:nth-child(2) {
            background: #fff;
            height: 380px;
        }
        .download-bar {
            margin-top: 15px;
            color: #fff;
        }
 
        .download-bar a {
            color: #fff;
            margin: 0 5px;
        }
 
        .download-bar a:hover {
            text-decoration: underline;
            color: #fff;
        }
    </style>
    <script>
        var isoldIE = false;
        if (navigator.userAgent.indexOf("MSIE") > 0) {
            if (navigator.userAgent.indexOf("MSIE 6.0") > 0
                || navigator.userAgent.indexOf("MSIE 7.0") > 0
                || avigator.userAgent.indexOf("MSIE 8.0") > 0) {
                isoldIE = true;
            }
        }
        if (window.top !== window.self) {
            window.top.location = window.location
        }
        ;
    </script>
    <script type="text/javascript"
            th:src="@{/js/plugin/jquery-2.1.4.min.js}"></script>
    <script type="text/javascript" th:src="@{/js/systools/MBase.js}"></script>
    <link th:href="@{/css/styleOne/login.min.css}" rel="stylesheet">
 
</head>
 
<body  >
<h3>hello socket</h3>
<p>【userId】:<div><input id="userId" name="userId" type="text" value="张三"></div>
<p>【toUserId】:<div><input id="toUserId" name="toUserId" type="text" value="李四"></div>
<p>【contentText】:<div><input id="contentText" name="contentText" type="text" value="hello websocket"></div>
<p>
<textarea id="messageBody" style="width:600px;height:300px;" >
 
</textarea>
</p>
 
 
<p>操作:<div><button onclick="openSocket()">开启socket</button></div>
<p>【操作】:<div><button onclick="sendMessage()">发送消息</button></div>
<p>【操作】:<div><button onclick="colseSocket()">关闭连接</button></div>
</body>
<script type="text/javascript" th:src="@{/js/systools/MJsBase.js}"></script>
    <script>
 
        var socket;
        function openSocket() {
            if(typeof(WebSocket) == "undefined") {
                console.log("您的浏览器不支持WebSocket");
            }else{
                console.log("您的浏览器支持WebSocket");
                //实现化WebSocket对象,指定要连接的服务器地址与端口  建立连接
                var userId = document.getElementById('userId').value;
                var socketUrl="ws://localhost:8080/webSocketServer?userId="+userId;
                console.log(socketUrl);
                if(socket!=null){
                    socket.close();
                    socket=null;
                }
                socket = new WebSocket(socketUrl);
                //打开事件
                socket.onopen = function() {
                    console.log("websocket已打开");
                    //socket.send("这是来自客户端的消息" + location.href + new Date());
                };
                //获得消息事件
                socket.onmessage = function(msg) {
                console.log(msg);
                    var serverMsg =  msg.data;
                    console.log(serverMsg);
                    
                    var serviceMesgObj=JSON.parse(serverMsg);
                    
                    //发现消息进入    开始处理前端触发逻辑
                    var messageBody= document.getElementById('messageBody');
                    messageBody.value=messageBody.value+"\r\n"+ serviceMesgObj.userId+":" + serviceMesgObj.messageBody;
                    
                    
                };
                //关闭事件
                socket.onclose = function() {
                    console.log("websocket已关闭");
                };
                //发生了错误事件
                socket.onerror = function() {
                    console.log("websocket发生了错误");
                }
            }
        }
 
        function sendMessage() {
            if(typeof(WebSocket) == "undefined") {
                console.log("您的浏览器不支持WebSocket");
            }else {
                // console.log("您的浏览器支持WebSocket");
                var targetUserId = document.getElementById('toUserId').value;
                var contentText = document.getElementById('contentText').value;
                var msg = '{"messageType":1,"targetUserId":"'+targetUserId+'","messageBody":"'+contentText+'"}';
                console.log(msg);
                socket.send(msg);
                 var userId = document.getElementById('userId').value;
                var messageBody= document.getElementById('messageBody');
                messageBody.value=messageBody.value+"\r\n"+ userId+":" + contentText;
                    
            }
        }
        function colseSocket() {
            if(typeof(WebSocket) == "undefined") {
                console.log("您的浏览器不支持WebSocket");
            }else {
                socket.close();
            }
        }
    </script>
 
 
</html>