前面说过,客户端websocket协议一般是html 或js页面处理的,今天我们也分析一下流程:
1、最简单的html页面建立websocket连接,并发送信息:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Netty 实时通信</title>
</head>
<body>
发送消息:<input type="text" id="msgContent" />
<input type="button" value="发送消息" onclick="CHAT.chat()" />
<hr />
接收消息:
<div id="receiveMsg"></div>
<script type="text/javascript">
window.CHAT = {
socket: null,
init:function(){
//判断浏览器是否支持websocket
if(window.WebSocket){
//创建websocket 对象
CHAT.socket = new WebSocket("ws://192.168.09.131:8888/ws");
CHAT.socket.onopen = function(){
console.log("链接建立成功");
},
CHAT.socket.close=function(){
console.log("链接关闭");
},
CHAT.socket.onerror = function(){
console.log("发生异常");
},
CHAT.socket.onmessage = function(e){
console.log("接受消息:"+e.data);
var receiveMsg = document.getElementById("receiveMsg");
var html= receiveMsg.innerHTML;//获取本对象原🈶️的内容
//嵌入新的内容
receiveMsg.innerHTML= html + "<br/>"+e.data;
}
}else{
console.log("您的浏览器不支持websocket协议");
}
},
chat:function(){
//获取发送消息框中所输入内容
var msgContent = document.getElementById("msgContent").value;
//将客户输入的消息进行发送
CHAT.socket.send(msgContent);
}
};
CHAT.init();
</script>
</body>
</html>
此接口发送的信息之间到这里,如下图:
2、升级版聊天页面:
<!doctype html>
<script src="../js/mui.js"></script>
<script src="../js/app.js"></script>
<script type="text/javascript">
mui.init();
var user ;
mui.plusReady(function () {
user= app.getUserGlobalInfo();
//加载好友请求记录
var thisWebview = plus.webview.currentWebview();
thisWebview.addEventListener("show",function(){
loadingFriendRequest();
});
//添加自定义事件,刷新好友请求
window.addEventListener("refresh",function(){
loadingFriendRequest();
CHAT.init();
});
//CHAT.init();
//为快照列表批量绑定触事件
mui("#ul_chatSnapshot").on("tap",".chat-with-friend",function(e){
var friendUserId = this.getAttribute("friendUserId");
var friendNickname = this.getAttribute("friendNickname");
var friendFaceImage = this.getAttribute("friendFaceImage");
//打开聊天页面
mui.openWindow({
url: "bird-chatting.html",
id: "bird-chatting" + friendUserId,//每个好友的聊天页面都是一一对应的
extras: {
friendUserId:friendUserId,
friendNickname:friendNickname,
friendFaceImage:friendFaceImage
}
});
var me = app.getUserGlobalInfo();
//标记未读状态为已读
app.readUserChatSnapShot(me.id,friendUserId);
//渲染快照到列表进行展示
loadingChatSnapshot();
});
//左滑删除聊天记录
mui("#ul_chatSnapshot").on("tap","#link_delChatRecord",function(e){
var me = this;
//获取朋友id
var friendUserId = me.getAttribute("friendUserId");
//1. 删除本地聊天记录
app.deleteUserChatHistory(user.id,friendUserId);
//2. 删除本地聊天快照
app.deleteUserChatSnapshot(user.id,friendUserId);
//移除当前用户操作的dom节点
var li = me.parentNode.parentNode;
var ul_chatSnapshot = document.getElementById("ul_chatSnapshot");
ul_chatSnapshot.removeChild(li);//删除li元素
});
});
window.CHAT = {
socket: null,
init:function(){
//判断浏览器是否支持websocket
if(window.WebSocket){
//如果当前WebSocket 状态已经连接,无需重复初始化WebSocket
if(CHAT.socket !=null && CHAT.socket !=undefined && CHAT.socket.readyState == WebSocket.OPEN){
return false;
}
//创建websocket 对象
try{
CHAT.socket = new WebSocket(app.nettyServerUrl);
CHAT.socket.onopen = CHAT.wsopen,
CHAT.socket.close=CHAT.wsclose,
CHAT.socket.onerror = CHAT.wserror,
CHAT.socket.onmessage = CHAT.wsmessage