最近在学 WebSocket,看到大家都是写im的demo,所以写个坦克大战的小demo
主要技术
- springWebSocket
//继承AbstractWebSocketHandler处理socket连接
public class TankWebSocket extends AbstractWebSocketHandler
//映射路径
<websocket:handlers>
<websocket:mapping path="/tank" handler="tankWS"/>
</websocket:handlers>
<bean id="tankWS" class="me.zingon.WebSocket.ws.TankWebSocket"/>
//建立连接
var url="ws://"+window.location.host+"${pageContext.request.contextPath}/tank"
sock=new WebSocket(url);
sock.onmessage=function(e){
console.log("message:",e.data);
draw(e.data);
}
sock.onclose=function(){
var con=document.getElementById("con");
con.innerHTML="";
layer.msg("咖喱 gay gay!(刷新重玩)");
}
完整代码
GitOSC:https://git.oschina.net/A_yes/WebSocketTank