websocket是实现实时信息传输的,建立较长时间的socket通讯,可以实现服务器的信息推送。
先创建一个WebSocket对象,指定要连接的服务器地址与端口
socket = new WebSocket("ws://127.0.0.1:8080/WebSocket/ws/mao");
WebSocket对象有几个方法:
onopen() 打开socket连接
onmessage() 获得消息事件
onclose() 关闭事件
onerror() 发生错误事件
send() 发送消息
代码
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" charset="UTF-8"/>
<title>WebSocket客户端</title>
</head>
<body>
<div>
<div id="area"></div>
<input type="button" id="btnConnection" value="连接" />
<input type="button" id="btnClose" value="关闭" />
<input type="button" id="btnSend" value="发送" />
</div>
<script src="js/jquery.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(function() {
var socket;
if(typeof(WebSocket) == "undefined") {
alert("您的浏览器不支持WebSocket");
return;
}
$("#btnConnection").click(function() {
//实现化WebSocket对象,指定要连接的服务器地址与端口
socket = new WebSocket("ws://127.0.0.1:8080/WebSocket/ws/mao");
//打开事件
socket.onopen = function() {
alert("Socket 已打开");
//socket.send("这是来自客户端的消息" + location.href + new Date());
};
//获得消息事件
socket.onmessage = function(msg) {
$("#area").html(msg.data)
};
//关闭事件
socket.onclose = function() {
alert("Socket已关闭");
};
//发生了错误事件
socket.onerror = function() {
alert("发生了错误");
}
});
$("#btnSend").click(function() {
//发送信息
socket.send("这是来自网页端的消息" + location.href + new Date());
});
$("#btnClose").click(function() {
socket.close();
});
});
</script>
</body>
</html>
后台代码也有与一样的方法
onOpen()
onMessage()
onClose()
onError()
sendMessage()
package action;
import java.io.IOException;
import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
//ws://127.0.0.1:8080/ws/mao
@ServerEndpoint("/ws/{user}")
public class WebSocketTest {
private String currentUser;
private Session session;
//连接打开时执行
@OnOpen
public void onOpen(@PathParam("user") String user, Session session) {
System.out.println("连接打开");
currentUser = user;
this.session = session;
try {
for(int i=0;i<10;i++){
sendMessage(""+i);
Thread.sleep(1000);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Connected ... " + session.getId());
}
//收到消息时执行
@OnMessage
public String onMessage(String message, Session session) {
System.out.println("收到消息时执行");
System.out.println(currentUser + ":" + message);
return currentUser + ":" + message+"这是返回值";
}
//连接关闭时执行
@OnClose
public void onClose(Session session, CloseReason closeReason) {
System.out.println("连接关闭");
System.out.println(String.format("Session %s closed because of %s", session.getId(), closeReason));
}
//连接错误时执行
@OnError
public void onError(Throwable t) {
System.out.println("连接错误");
t.printStackTrace();
}
public void sendMessage(String message) throws IOException{
this.session.getBasicRemote().sendText(message);
}
}
一个不错介绍websocket的博客点击打开链接