69 lines
1.9 KiB
Plaintext
69 lines
1.9 KiB
Plaintext
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<title></title>
|
|
</head>
|
|
<body>
|
|
<div>
|
|
<input id="nickName" name="nickName" type="text" value="Jone" />
|
|
</div>
|
|
<div>
|
|
<input id="btnConnection" name="btnConnection" value="Connection" type="button" />
|
|
<input id="btnDisconnect" name="btnDisconnect" value="Disconnect" type="button" />
|
|
</div>
|
|
<div>
|
|
<input id="btnSend" name="btnSend" value="Send" type="button" />
|
|
</div>
|
|
<div id="message">
|
|
|
|
</div>
|
|
|
|
<script>
|
|
window.onload = function () {
|
|
document.getElementById("btnConnection").onclick = Connection;
|
|
document.getElementById("btnDisconnect").onclick = Disconnect;
|
|
document.getElementById("btnSend").onclick = Send;
|
|
//CreateWebSocket("ws://localhost/WebApiDemo/api/chat?nickName=b4s51x2");
|
|
}
|
|
|
|
var webSocket;
|
|
|
|
function Connection() {
|
|
webSocket = new WebSocket("ws://localhost/WebApiDemo/api/chat?nickName=b4s51x2");
|
|
InitEventHandles();
|
|
}
|
|
|
|
function InitEventHandles() {
|
|
//建立成功
|
|
webSocket.onopen = function () {
|
|
console.log("socket opened");
|
|
}
|
|
//异常
|
|
webSocket.onerror = function () {
|
|
console.log("An exception hai occurred");
|
|
}
|
|
//服务端返回消息回调
|
|
webSocket.onmessage = function (event) {
|
|
console.log(event.data);
|
|
}
|
|
//关闭连接回调
|
|
webSocket.onclose = function () {
|
|
console.log("socket closed");
|
|
}
|
|
}
|
|
|
|
function Send() {
|
|
//向服务器发送数据
|
|
webSocket.send(document.getElementById("nickName").value);
|
|
}
|
|
|
|
function Disconnect() {
|
|
webSocket.close();
|
|
}
|
|
|
|
|
|
</script>
|
|
</body>
|
|
</html>
|