webSocket处理类
com.zr.platform.websocketjava.io.IOExceptionjava.util.Setjava.util.concurrent.CopyOnWriteArraySetjava.util.concurrent.atomic.AtomicIntegerjavax.websocket.javax.websocket.javax.websocket.javax.websocket.Sessionjavax.websocket.server.(=) WebSocketController { String = AtomicInteger = AtomicInteger()Set<WebSocketController> = CopyOnWriteArraySet<>()String =Session () { = + .getAndIncrement()} (Session session) { .= session.add()System..println(+ session.getId())String message = String.()(message)} (Session session) { System..println(+ session.getId()).remove()String message = String.()(message)} (String messageSession session) { System..println(+message++session.getId())String response = String.(message)(response)} (String msg) { (WebSocketController client : ) { { (client) { client..getBasicRemote().sendText(msg)} } (IOException e) { System..println().remove(client){ client..close()} (IOException e1) { } String message = String.(client.)(message)} } } }
新加一个Handler拦截websocket请求
com.zr.platform.websocketjava.util.regex.Patternjavax.servlet.http.HttpServletRequestjavax.servlet.http.HttpServletResponsecom.jfinal.handler.Handlercom.jfinal.kit.StrKitWebSocketHandler Handler{ Pattern (String filterUrlRegx) { (StrKit.(filterUrlRegx)) IllegalArgumentException()= Pattern.(filterUrlRegx)} (String targetHttpServletRequest requestHttpServletResponse response[] isHandled) { (.matcher(target).find()) .handle(targetrequestresponseisHandled)} }
在JFinal配置文件里配置 (MainConfig)
(Handlers me) { me.add(WebSocketHandler())}
客户端页面
websocket = (in window){ websocket = WebSocket()} { () } websocket.= (){ console.()}websocket.= (event){ console.()} websocket.= (event){ console.(event.data)} websocket.= (){ console.()} window.= (){ websocket.()} (innerHTML){ document.().+= innerHTML + } (){ websocket.()} (){ message = document.().valuewebsocket.(message)}
最后想知道服务端怎么主动给客户端发送消息?在其他类中怎么调用webSocket里的方法?
项目:JBolt极速开发平台