使用websocket编写一个聊天程序,包括上线通知和点对点聊天
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

35 lines
908 B

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button onclick="sendHandler('zhangsan')">给zhangsan发送消息</button>
<button onclick="sendHandler('wangwu')">给wangwu发送消息</button>
<script>
// const token = ""
// var ws = new WebSocket("ws://192.168.0.70:8801/ws/imserver?token=" + token)
var ws = new WebSocket("ws://192.168.0.70:8999/ws/imserver/lisi")
ws.open = function (msg) {
console.log("open:" + msg)
}
ws.onmessage = function (msg) {
console.log("message:" + msg)
}
function sendHandler(userid) {
var param = {}
param.sendUserId = 'lisi'
param.receiveUserId = userid
param.message = '来自 lisi 消息'
param.msgType = 2
param.uuid = new Date() * 1
ws.send(JSON.stringify(param))
}
</script>
</body>
</body>
</html>