参考网上的在线写个html,以后就不用找了。
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
地址:<input type="text" id='wsaddr' style='width:300px' value="ws://127.0.0.1:8198">
<button type="button" class="btn btn-default" onclick='addsocket();'> 连接</button>
<button type="button" class="btn btn-default" onclick='closesocket();'> 断开</button>
<button type="button" class="btn btn-default" onclick='$("#wsaddr").val("")'> 清空</button>
<button type="button" class="btn btn-default" onclick='$("#wsaddr").val("ws://localhost:9010/ajaxchattest")'> 示例</button>
<div id="output" style="border:1px solid #ccc;height:365px;overflow: auto;margin-left:15px"></div>
<input type="text" id='message' class="form-control" style='width:810px' placeholder='待发信息' onkeydown="en(event);">
<button class="btn btn-default" type="button" onclick="doSend();">发送</button>
</body>
<script>
function formatDate(now) {
var year = now.getFullYear();
var month = now.getMonth() + 1;
var date = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
return year + "-" + (month = month < 10 ? ("0" + month) : month) + "-" + (date = date < 10 ? ("0" + date) : date) + " "
+ (hour = hour < 10 ? ("0" + hour) : hour) + ":" + (minute = minute < 10 ? ("0" + minute) : minute) + ":"
+ (second = second < 10 ? ("0" + second) : second);
}
/**
* 建立连接
*/
function addsocket() {
var wsaddr = $("#wsaddr").val();
console.log(wsaddr);
if (wsaddr == '') {
alert("请填写websocket的地址");
return false;
}
StartWebSocket(wsaddr);
}
/**
* 关闭连接
*/
function closesocket() {
websocket.close();
}
/**
* 开启websocket
*/
function StartWebSocket(wsUri) {
websocket = new WebSocket(wsUri);
websocket.onopen = function (evt) {
onOpen(evt)
};
websocket.onclose = function (evt) {
onClose(evt)
};
websocket.onmessage = function (evt) {
onMessage(evt)
};
websocket.onerror = function (evt) {
onError(evt)
};
}
function onOpen(evt) {
writeToScreen("<span style='color:red'>连接成功,现在你可以发送信息啦!!!</span>");
}
function onClose(evt) {
writeToScreen("<span style='color:red'>websocket连接已断开!!!</span>");
websocket.close();
}
function onMessage(evt) {
//json格式的返回处理
//var json = JSON.parse(evt.data);
//console.log(json);
//var content = json.text
//writeToScreen('<span style="color:blue">服务端回应 ' + formatDate(new Date()) + '</span><br/><span class="bubble">' + content + '</span>');
//原始写法
writeToScreen('<span style="color:blue">服务端回应 '+formatDate(new Date())+'</span><br/><span class="bubble">'+ evt.data+'</span>')
}
function onError(evt) {
writeToScreen('<span style="color: red;">发生错误:</span> ' + evt.data);
}
function doSend() {
var message = $("#message").val();
console.log(message);
if (message == '') {
alert("请先填写发送信息");
$("#message").focus();
return false;
}
if (typeof websocket === "undefined") {
alert("websocket还没有连接,或者连接失败,请检测");
return false;
}
if (websocket.readyState == 3) {
alert("websocket已经关闭,请重新连接");
return false;
}
console.log(websocket);
$("#message").val('');
writeToScreen('<span style="color:green">你发送的信息 ' + formatDate(new Date()) + '</span><br/>' + message);
websocket.send(message);
}
function writeToScreen(message) {
var div = "<div class='newmessage'>" + message + "</div>";
var d = $("#output");
var d = d[0];
var doScroll = d.scrollTop == d.scrollHeight - d.clientHeight;
$("#output").append(div);
if (doScroll) {
d.scrollTop = d.scrollHeight - d.clientHeight;
}
}
function en(event) {
var evt = evt ? evt : (window.event ? window.event : null);
if (evt.keyCode == 13) {
doSend()
}
}
</script>
</html>
?参考地址:http://coolaf.com/tool/chattest
|