Skip to content

Commit

Permalink
WebSocket测试前端-交易私聊及订单提醒
Browse files Browse the repository at this point in the history
WebSocket测试前端-交易私聊及订单提醒
  • Loading branch information
Inetgeek committed Oct 5, 2022
1 parent 65c9e84 commit a25fe0f
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ws_frontend/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions ws_frontend/.idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions ws_frontend/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions ws_frontend/.idea/ws_frontend.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions ws_frontend/chat.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
我的id: <input type="text" id="myId"><button onclick="link()">连接服务器</button><br>
<hr>
目标id: <input type="text" id="toId"><br>
内容: <input type="text" id="content">
<input type="button" value="发送" onclick="send()"><br>
<hr>
收到消息: <div id="msg"></div>

</body>
<script>
var ws;
function link() {
if (ws != null) {
ws.close();
ws=null;
}
var myId = document.getElementById("myId").value;
ws=new WebSocket('ws://localhost:8080/api/v1/ws/chat/'+myId);
ws.onmessage=function (msg) {
var data=msg.data;
console.log(data);
var old=document.getElementById("msg").innerHTML
document.getElementById("msg").innerHTML=old+"<br>"+msg.data;
};
ws.onopen=function (ev) {
console.log("服务器连接成功...")
};
}
function send(){
var myId=document.getElementById("myId").value;
var toId=document.getElementById("toId").value;
var content=document.getElementById("content").value;
ws.send(myId+'@'+toId+"@"+content);
}
</script>
</html>
47 changes: 47 additions & 0 deletions ws_frontend/notice.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
消息展示区:<br/>
<div id="textArea"></div>

</body>
<script>
var textArea = document.getElementById('textArea');


var websocket = null;
//如果浏览器支持websocket就建立一个websocket,否则提示浏览器不支持websocket
if('websocket' in window){
websocketPage = new WebSocket('ws://localhost:8080//api/v1/ws/notice/20220921112424');
}else{
alert('浏览器不支持websocket!');
}
//建立websocket时自动调用
websocketPage.onopen = function (event) {
console.log('建立连接');
}
//关闭webscoket时自动调用
websocketPage.oncolse = function (event){
console.log('关闭连接');
}
//websocket接收到消息时调用
websocketPage.onmessage = function (event){
//将接收到的消息展示在消息展示区
textArea.innerText += event.data;
textArea.innerHTML += "<br/>";
}
//websocket出错自动调用
websocketPage.onerror = function () {
alert('websocket出错');
}
//关闭窗口前关闭websocket连接
window.onbeforeunload = function (){
websocketPage.close();
}

</script>
</html>

0 comments on commit a25fe0f

Please sign in to comment.