1、使用框架spring+springmvc+mybatis+jdk7+tomcat7+maven

2、基本原理:

    a. WebSocket协议是一种双向通信协议,它建立在TCP之上,同http一样通过TCP来传输数据,但是 它和http最大的不同有两点:

1.WebSocket是一种双向通信协议,在建立连接后,WebSocket服务器和Browser/UA都能主动地向对方发送或接收数据,就像Socket一样,不同的是WebSocket是一种建立在Web基础上的一种简单模拟Socket的协议;

2.WebSocket需要通过握手连接,类似于TCP它也需要客户端和服务器端进行握手连接,连接成功后才能相互通信。

    b.struts   和 websocket  是2条处理请求的路,被struts 匹配上了 ,自然就到不了 websocket的处  理逻辑,(http和ws是两种不同的请求协议,注意框架的拦截器(过滤器))

3、客户端:

<html>
<head>
<title>Title</title>
<style> div {
margin-left: auto;
margin-right: auto;
} </style>
</head>
<body onLoad="startWebSocket()">
<script type="text/javascript">
function clog(title) {
console.log(title);
} var ws = null;
function startWebSocket() { if ('WebSocket' in window) { try {
ws = new WebSocket("ws://127.0.0.1:80/websocket/tui");
} catch (e) {
clog("1");
}
} else if ('MozWebSocket' in window) {
ws = new MozWebSocket("ws://127.0.0.1:80/websocket/tui");
} else {
clog("websocket not support");
} ws.onmessage = function (evt) {
say(evt.data);
}; ws.onclose = function (evt) {
clog("close!");
}; ws.onopen = function (evt) {
clog("open");
};
} function sendMsg() {
ws.send(document.getElementById('writeMsg').value);
} function say(msg) {
var div = document.createElement("div");
div.innerHTML = msg;
document.body.appendChild(div);
} </script>
<div onClick="say('d')">WebSocket聊天室</div>
<div style="border:1px solid #09F"></div>
<input type="text" id="writeMsg"/>
<input type="button" value="send" onClick="sendMsg()"/> </body>
</html>

4、服务端:

@ServerEndpoint(value = "/websocket/tui")
public class WebSocketDemo{
private static final String GUEST_PREFIX = "Guest";
private static final AtomicInteger connectionIds = new AtomicInteger(0);
private static final Set<WebSocketDemo> connections = new HashSet<>(); private final String nickname;
private Session session; public WebSocketDemo() {
nickname = GUEST_PREFIX + connectionIds.getAndIncrement();
} //建立连接
@OnOpen
public void start(Session session) {
this.session = session;
connections.add(this);
String message = String.format("* %s %s", nickname, "has joined.");
System.out.println(message);
} //接受消息
@OnMessage
public void incoming(String message) {
System.out.println(message.toString());
//broadcast(filteredMessage);
broadcast(message.toString());
} //客户端关闭了连接
@OnClose
public void end() {
connections.remove(this);
String message = String.format("* %s %s", nickname, "has disconnected.");
System.out.println(message);
//broadcast(message);
} //WebSocket服务出错
@OnError
public void onError(Throwable t){
//log.error("Chat Error: " + t.toString(), t);
System.out.println("server has an error!");
} private static void broadcast(String msg) {
for (WebSocketDemo client : connections) {
try {
synchronized (client) {
client.session.getBasicRemote().sendText(msg);
}
} catch (IOException e) {
//log.debug("Chat Error: Failed to send message to client", e);
connections.remove(client);
try {
client.session.close();
} catch (IOException e1) {
// Ignore
}
String message = String.format("* %s %s",
client.nickname, "has been disconnected.");
broadcast(message);
}
}
}
}

注意事项:

1、访问拦截器

2、注意部署的包冲突,例如:javax.websocket只是编译需要,而发布不需要,maven依赖注意适用范围

附:

<!--websocket编译需要,发布不需要-->
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-coyote -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-coyote</artifactId>
<version>7.0.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-juli -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-juli</artifactId>
<version>8.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.websocket/javax.websocket-api -->
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>

jee websocket搭建总结的更多相关文章

  1. 基于SpringBoot+WebSocket搭建一个简单的多人聊天系统

    前言   今天闲来无事,就来了解一下WebSocket协议.来简单了解一下吧. WebSocket是什么   首先了解一下WebSocket是什么?WebSocket是一种在单个TCP连接上进行全双工 ...

  2. nodejs+mongoose+websocket搭建xxx聊天室

    简介 本文是由nodejs+mongoose+websocket打造的一个即时聊天系统:本来打算开发一个类似于网页QQ类似功能的聊天系统,但是目前只是开发了一个模块功能 --- 类似群聊的,即一对多的 ...

  3. php+websocket搭建简易聊天室实践

    1.前言 公司游戏里面有个简单的聊天室,了解了之后才知道是node+websocket做的,想想php也来做个简单的聊天室.于是搜集各种资料看文档.找实例自己也写了个简单的聊天室. http连接分为短 ...

  4. iis WebSocket 搭建环境及配置

    http://www.86y.org/art_detail.aspx?id=816       WebSocket是html5新增加的一种通信协议,目前流行的浏览器都支持这个协议,例如Chrome,S ...

  5. 使用Html5下WebSocket搭建简易聊天室

    一.Html5WebSocket介绍 WebSocket protocol 是HTML5一种新的协议(protocol).它是实现了浏览器与服务器全双工通信(full-duplex). 现在,很多网站 ...

  6. Spring Boot中使用Websocket搭建即时聊天系统

    1.首先在pom文件中引入Webscoekt的依赖 <!-- websocket依赖 --> <dependency> <groupId>org.springfra ...

  7. 基于websocket搭建简易群聊

    1.前端HTML <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&qu ...

  8. websocket搭建的聊天室

    在前后端数据交互的时候我们经常使用的是ajax,用的是传统的http协议,而http协议有个致命的缺点,就是请求一结束,连接就断开了, 我们为了保持这个链接的,通常会使用cookie,而自从h5出现w ...

  9. WebSocket 搭建简单聊天网站

    HTML5 WebSocket WebSocket是HTML5开始提供的一种在单个 TCP 连接上进行全双工通讯的协议. 在WebSocket API中,浏览器和服务器只需要做一个握手的动作,然后,浏 ...

随机推荐

  1. Daily Scrum Meeting ——SixthDay

    一.Daily Scrum Meeting照片 佳恺请假了...可能去约会了罢 二.Burndown Chart 欣慰,但是还是感到"鸭梨山大"! 三.项目进展 1.活动列表查询功 ...

  2. Latex制作beamer

    Latex制作beamer latex beamer Beamer Theme Matrix网页给出了一般常用的主题和配色方案. tuwcvl这个主题比较简单,感觉比较适合用作实验室内的报告,可以自己 ...

  3. sketchup

    1. clean start 1. 删除中间人物 2. windows---style 3. Windows---Model Info 2. 好的建模习惯 1. 正面朝镜头 View---ToolBa ...

  4. Spatial Transformer Networks(空间变换神经网络)

    Reference:Spatial Transformer Networks [Google.DeepMind]Reference:[Theano源码,基于Lasagne] 闲扯:大数据不如小数据 这 ...

  5. WebRTC手记之初探

    转载请注明出处:http://www.cnblogs.com/fangkm/p/4364553.html WebRTC是HTML5支持的重要特性之一,有了它,不再需要借助音视频相关的客户端,直接通过浏 ...

  6. 尝试封装适用于权限管理的通用API

    谈谈我对权限系统的简单理解 最近一段时间在研究权限系统,在园子里看到个很牛逼的开源的基于DDD-Lite的权限管理系统,并有幸加入了作者的QQ群,呵呵,受到了很大的影响.对于权限管理我有我自己的一些简 ...

  7. ZeroMQ接口函数之 :zmq_msg_init_size - 使用一个指定的空间大小初始化ZMQ消息对象

    ZeroMQ 官方地址 :http://api.zeromq.org/4-1:zmq_msg_init_size zmq_msg_init_size(3) ØMQ Manual - ØMQ/3.2.5 ...

  8. Unity: Passing Constructor Parameters to Resolve

    In this tutorial we will go through of couple different ways of using custom constructor parameters ...

  9. Learn ZYNC (2)

    AXI HP接口的DMA+GIC编程(参照博客) 参照文档:UG873,博客文档 参考设计代码文件:ug873源码 我的Vivado+SDK工程文件打包(60+M) 我的DMA驱动程序(已完成) Vi ...

  10. 动态调用webservice(部分转载)

    动态调用webservice,做个笔记: public class WSHelper { /// < summary> /// 动态调用web服务 /// < /summary> ...