<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>${currPatient.name}</title>
<meta name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta HTTP-EQUIV="pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<meta HTTP-EQUIV="expires" CONTENT="0">
<link href="${ctx}/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="${ctx}/css/site.css">
<script src="${ctx}/js/jquery.min.js"></script>
<script src="${ctx}/js/bootstrap.min.js"></script>
<script src="${ctx}/js/site.js"></script>
<script src="${ctx}/js/jweixin-1.0.0.js"></script>
<script src="${ctx}/layer/layer.min.js"></script>
<script type="application/javascript">
$(function(){
/* 自己 */
var userid="120"
var Chat = {};
Chat.socket = null;
// 创建一个websocket实例
Chat.connect = (function(host) {
if ('WebSocket' in window) {
Chat.socket = new WebSocket(host);
} else if ('MozWebSocket' in window) {
Chat.socket = new MozWebSocket(host);
} else {
Console.log('Console.log:你的浏览器不支持WebSocket');
return;
}
Chat.socket.onopen = function(){
//Console.log('Console.log:WebSocket链接打开');
//按下回车键发送消息
document.getElementById('chat').onkeydown = function(event) {
if (event.keyCode == 13) {
Chat.sendMessage();
}
};
document.getElementById('send').onclick = function(event) {
Chat.sendMessage();
};
};
Chat.socket.onclose = function () {
document.getElementById('chat').onkeydown = null;
Console.log('Console.log:WebSocket前端链接关闭');
};
Chat.socket.onmessage = function (message) {
//Console.log(message.data);
var sendMeId=message.data.substr(message.data.lastIndexOf("-")+1,message.data.length-1);
var sendMeContent=message.data.substr(0,message.data.lastIndexOf("-"));
if(sendMeId==userid){
showMeContent(sendMeContent, true);
}else{
showYouContent(sendMeContent, true)
}
};
});
Chat.initialize = function() {
//链接地址选择聊天页面的URL
if (window.location.protocol == 'http:') {
Chat.connect("ws://" + window.location.host + "/happy/websocket/"+userid);
} else {
Chat.connect("wss://" + window.location.host + "/happy/websocket/"+userid);
}
};
//发送消息函数,后面动态添加了发送好友的唯一ID
Chat.sendMessage = (function() {
var fid = $("#happynessId").val();
var messageContain = document.getElementById('chat').value;
var message = messageContain +"-"+fid;
if(fid==""){
alert("未选择发送消息的好友!");
return;
}else{
if (messageContain != "") {
Chat.socket.send(message);
document.getElementById('chat').value = '';
}else{
alert("发送消息不能为空!");
}
}
});
var Console = {};
Console.log = (function(message) {
var console = document.getElementById('console');
var p = document.createElement('p');
p.style.wordWrap = 'break-word';
p.innerHTML = message;
console.appendChild(p);
console.scrollTop = console.scrollHeight;
});
//初始化函数
Chat.initialize();
/* document.addEventListener("DOMContentLoaded", function() {
var noscripts = document.getElementsByClassName("noscript");
for (var i = 0; i < noscripts.length; i++) {
noscripts[i].parentNode.removeChild(noscripts[i]);
}
}, false); */
function showMeContent(msg, isScroll) {
var docPortrait = $('#docPortrait').val();
var _str = new Array();
_str.push('<div class="me">');
if (!docPortrait) {
_str.push('<img class="head-pic" src="img/1toux.png" alt="" style="width:40px;height:40px;border-radius: 50%;">');
} else if (docPortrait.substring(0, 4) == "http") {
_str.push('<img class="head-pic" src=' + docPortrait + ' alt="" style="width:40px;height:40px;border-radius: 50%;">');
} else {
_str.push('<img class="head-pic" src=' + docPortrait + ' alt="" style="width:40px;height:40px;border-radius: 50%;">');
}
_str.push('<p style="margin-right: 5em;max-width: 80%; word-break: break-word;">');
_str.push(msg.createdTimeStr + ':<br>');
if (msg.type == 1) {
_str.push(msg.content);
} else if (msg.type == 2) {
_str.push('<img class="img-itemClick" pic-url="'+msg.content+'" src='+msg.content+' alt="" width="120" height="120">');
} else if (msg.type == 3) {
_str.push('<img class="audio-itemClick" src="img/audio3.png" alt>');
} else {
_str.push(msg);
}
_str.push('</p>');
_str.push('</div>');
if (isScroll) {
$('.content').append(_str.join(''));
scrollBottom();
} else {
$('.content').prepend(_str.join(''));
scrollTop();
}
};
function showYouContent(msg, isScroll) {
var _str = new Array();
var patPortrait = $('#patPortrait').val();
_str.push('<div class="you">');
if (!patPortrait) {
_str.push('<img class="head-pic" src="img/1toux.png" alt="" style="width:40px;height:40px;border-radius: 50%;">');
} else if (patPortrait.substring(0, 4) == "http") {
_str.push('<img class="head-pic" src=' + patPortrait + ' alt="" style="width:40px;height:40px;border-radius: 50%;">');
} else {
_str.push('<img class="head-pic" src=' + patPortrait + ' alt="" style="width:40px;height:40px;border-radius: 50%;">');
}
_str.push('<p style="margin-left: 5em;max-width: 80%; word-break: break-word;">');
_str.push(msg.createdTimeStr + ':<br>');
if (msg.type == 1) {
_str.push(msg.content);
} else if (msg.type == 2) {
_str.push('<img class="img-itemClick" pic-url="' + msg.picSrc + '" src=' + msg.picSrc + ' alt="" width="120" height="120">');
} else if (msg.type == 3) {
_str.push('<img class="audio-itemClick" src="img/audio3.png" alt>');
} else {
_str.push(msg);
}
_str.push('</p>');
_str.push('</div>');
if (isScroll) {
$('.content').append(_str.join(''));
scrollBottom();
} else {
$('.content').prepend(_str.join(''));
scrollTop();
}
};
function scrollBottom(){
$('html, body, .content').animate({scrollTop: $(document).height()}, 300);
}
function scrollTop(){
$('html, body, .content').animate({scrollTop:0});
}
})
</script>
<%-- <script type="text/javascript">
//自己的ID
//var userid =document.getElementById('happynessId').value;
var userid ="120";
var websocket = null;
//判断当前浏览器是否支持WebSocket
if ('WebSocket' in window) {
websocket = new WebSocket("ws://localhost:8080/happy/websocket/"+userid);
}
else {
alert('当前浏览器 Not support websocket')
}
//连接发生错误的回调方法
websocket.onerror = function () {
setMessageInnerHTML("WebSocket连接发生错误");
};
//连接成功建立的回调方法
websocket.onopen = function () {
setMessageInnerHTML("WebSocket连接成功");
}
//接收到消息的回调方法
websocket.onmessage = function (event) {
setMessageInnerHTML(event.data);
}
//连接关闭的回调方法
websocket.onclose = function () {
setMessageInnerHTML("WebSocket连接关闭");
}
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
window.onbeforeunload = function () {
closeWebSocket();
}
//将消息显示在网页上
function setMessageInnerHTML(innerHTML) {
document.getElementById('message').innerHTML += innerHTML + '<br/>';
}
//关闭WebSocket连接
function closeWebSocket() {
alert("关闭");
websocket.close();
}
//发送消息
function send() {
//发送人的ID
var fid =document.getElementById('happynessId').value;
var messageContain = document.getElementById('chat').value;
var message = messageContain +"-"+fid;
if(fid==""){
alert("未选择发送消息的好友!");
return;
}else{
if (messageContain != "") {
websocket.send(message);
document.getElementById('chat').value = '';
}else{
alert("发送消息不能为空!");
}
}
/* var message = document.getElementById('text').value;
websocket.send(message); */
}
</script> --%>
</head>
<!-- <body class="normal-ky-bg">
<input name="happynessId" id="happynessId" type="hidden" value="110" />
<div>
<p>
<input type="text" placeholder="输入" id="chat"/>
</p>
<div id="console-container">
<div id="message"></div>
</div>
<button onclick="send()">发送</button>
</div> -->
<body class="normal-ky-bg">
<!--别人id -->
<input name="happynessId" id="happynessId" type="hidden" value="110" />
<p></p>
<div id="console"></div>
<div class="container no-padding-bm">
<div class="col-xs-12 talk">
<div class="space12"></div>
<div class="text-center">
<span id="load_more" class="ky-label gray" onclick="viewMore()">查看更多消息</span>
</div>
<div class="content"></div>
<div class="send-area" id="send-area">
<div class="fixed">
<div style="width: 10%; display: inline; float: left;">
<span><img id="record" alt="" src="img/sd.png"></span>
</div>
<div style="width: 50%; display: inline; float: left;">
<input class="form-control" id="chat" type="text" />
</div>
<div style="width: 10%; display: inline;">
<button class="btn btn-default ac-btn" id="send" type="button">发送</button>
</div>
<div style="width: 10%; display: inline;">
<button class="btn btn-default ac-btn" id="choice" type="button"
onclick="my()">图片</button>
</div>
</div>
</div>
</div>
</div>
<div id="wxRecordPlugin"></div>
</body>
</html>
websocket
最新推荐文章于 2021-12-27 07:52:14 发布