星空网 > 软件开发 > Java

利用webSocket使网页和服务器通信

WebSocket protocol 是HTML5一种新的协议。它实现了浏览器与服务器全双工通信(full-duplex)。具体说明请查阅相关资料,下面我来演示一种简单的页面与服务器通信的简单样例。

新建个web工程(基于tomcat-7版本(6以下的版本未实现webSocket功能))

引入tomcat/lib目录下的tomcat7-websocket.jar和websocket-api.jar添加到classpath中

新建WebSocketConfig.java如下

本次采用注解方式

import java.util.Set;import javax.websocket.Endpoint;import javax.websocket.server.ServerApplicationConfig;import javax.websocket.server.ServerEndpointConfig;public class WebSocketConfig implements ServerApplicationConfig{  @Override  public Set<ServerEndpointConfig> getEndpointConfigs(      Set<Class<? extends Endpoint>> scanned) {    return null;  }  @Override  public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanned) {    //返回scanned    return scanned;  }}

编写EchoSocket代码

import javax.websocket.OnClose;import javax.websocket.OnMessage;import javax.websocket.OnOpen;import javax.websocket.Session;import javax.websocket.server.ServerEndpoint;@ServerEndpoint("/zqq/echo")public class EchoSocket {    public EchoSocket() {    super();  }  @OnOpen  public void open(Session session){    System.out.println("id " + session.getId());  }    @OnMessage  public void message(Session session,String msg){    System.out.println("sessionid " + session.getId() + " : " + msg);  }}

此时编写客户端echo.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>My JSP 'open.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--  <link rel="stylesheet" type="text/css" href="styles.css">  --><script type="text/javascript">  var ws;
  //websocket,协议开头为ws var target = "ws://${pageContext.request.remoteHost}:8080${pageContext.request.contextPath}/zqq/echo"; function subOpen() { if ('WebSocket' in window) { ws = new WebSocket(target); } else if ('MozWebSocket' in window) { ws = new MozWebSocket(target); } else { alert('WebSocket is not supported by this browser.'); return; } ws.onmessage=function(event){ console.info(event.data); console.info(document.getElementById("h1").value); document.getElementById("h1").innerHTML+=event.data+" "; }; } function subSend(){ var text = document.getElementById("input").value; //alert(text); ws.send(text); document.getElementById("input").value=""; }</script></head><body> <button onclick="subOpen()">点击开启</button><br/> <input id="input"><br/> <button onclick="subSend()">发送</button></body></html>

此时将程序部署到tomcat上就可以访问了,记得要先打开通道,输入文字就行

第一次写东西,如有错误还请指教,继续学习中。。。。

 




原标题:利用webSocket使网页和服务器通信

关键词:web

web
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

帮助页面:https://www.goluckyvip.com/tag/28838.html
棒谷科技Banggood:https://www.goluckyvip.com/tag/28839.html
海关最新宣布:https://www.goluckyvip.com/tag/2884.html
包裹交付量:https://www.goluckyvip.com/tag/28840.html
包裹准点交付率:https://www.goluckyvip.com/tag/28841.html
包装法:https://www.goluckyvip.com/tag/28842.html
川藏线自驾游要怎么走才比较划算呢?:https://www.vstour.cn/a/411240.html
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流