使用Tomcat8实现WebSocket的简单例子
2015-11-16 13:27
435 查看
运行环境: MyEclipse 2014+Tomcat 8
建立Web Project工程如下图所示:
如果 Add New Runtime处添加的是Tomcat 8的话就不需要导入新的Jar包了。
编写WebSocket的Java程序
@ServerEndpoint("/java/socket") //使用注解建立连接点
public class Progress
{
@OnMessage //接受客户端消息
public void onMessage(String msg, Session session) throws Exception
{
System.out.println("msg : " + msg);
System.out.println(session.getQueryString());
String tmp = "让我们定义一个 Tomcat websocket服务器端";
System.out.println("send msg : " + tmp);
session.getBasicRemote().sendText(tmp); //发送信息到客户端
session.close();
//关闭连接点
}
@OnOpen //成功连接时执行此代码
public void onOpen()
{
System.out.println("------------onOpen------------");
}
@OnClose //连接关闭时执行
public void onClose()
{
System.out.println("------------onClose------------");
}
}
JSP代码如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.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">
-->
</head>
<body>
<button onclick="start()">测试</button>
<div id="messages"></div>
<script type="text/javascript">
var webSocket = null;
function init()
{
webSocket = new WebSocket('ws://127.0.0.1:8082/WebSocketAndTomcat/java/socket'); //建立连接点 WebSocketAndTomcat要换成自己的项目名
webSocket.onerror = function(event)
{
onError(event);
};
webSocket.onopen = function(event)
{
onOpen(event);
};
webSocket.onmessage = function(event) //接受客户端消息
{
onMessage(event);
};
}
function onMessage(event)
{
document.getElementById('messages').innerHTML += '<br />' + event.data;
}
function onOpen(event)
{
document.getElementById('messages').innerHTML = 'Connection established';
webSocket.send("ds"); //发送消息给服务器端
}
function onError(event)
{
alert(event.data);
}
function start()
{
init();
}
</script>
</body>
</html>
这个例子很简单,如果您刚接触到WebSocket技术,可以用来入门。
建立Web Project工程如下图所示:
如果 Add New Runtime处添加的是Tomcat 8的话就不需要导入新的Jar包了。
编写WebSocket的Java程序
@ServerEndpoint("/java/socket") //使用注解建立连接点
public class Progress
{
@OnMessage //接受客户端消息
public void onMessage(String msg, Session session) throws Exception
{
System.out.println("msg : " + msg);
System.out.println(session.getQueryString());
String tmp = "让我们定义一个 Tomcat websocket服务器端";
System.out.println("send msg : " + tmp);
session.getBasicRemote().sendText(tmp); //发送信息到客户端
session.close();
//关闭连接点
}
@OnOpen //成功连接时执行此代码
public void onOpen()
{
System.out.println("------------onOpen------------");
}
@OnClose //连接关闭时执行
public void onClose()
{
System.out.println("------------onClose------------");
}
}
JSP代码如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.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">
-->
</head>
<body>
<button onclick="start()">测试</button>
<div id="messages"></div>
<script type="text/javascript">
var webSocket = null;
function init()
{
webSocket = new WebSocket('ws://127.0.0.1:8082/WebSocketAndTomcat/java/socket'); //建立连接点 WebSocketAndTomcat要换成自己的项目名
webSocket.onerror = function(event)
{
onError(event);
};
webSocket.onopen = function(event)
{
onOpen(event);
};
webSocket.onmessage = function(event) //接受客户端消息
{
onMessage(event);
};
}
function onMessage(event)
{
document.getElementById('messages').innerHTML += '<br />' + event.data;
}
function onOpen(event)
{
document.getElementById('messages').innerHTML = 'Connection established';
webSocket.send("ds"); //发送消息给服务器端
}
function onError(event)
{
alert(event.data);
}
function start()
{
init();
}
</script>
</body>
</html>
这个例子很简单,如果您刚接触到WebSocket技术,可以用来入门。
相关文章推荐
- java-模拟tomcat服务器
- i-jetty环境搭配与编译
- 实现单Tomcat多Server配置
- 生产环境下的Tomcat配置
- 2015-2016网页设计趋势分析 Web Design of Trends
- Linux部署Tomcat服务器
- jenkins------结合maven将svn项目自动部署到tomcat下
- 如何搞定tomcat这只喵~
- tomcat在opensuse下开机自启失败的原因分析及解决方法
- jsp项目中更改tomcat的默认index.jsp访问路径的方法
- Tomcat 多端口 多应用
- tomcat 5.0 + apache 2.0 完全安装步骤详解
- Tomcat安全设置 win2003 下tomcat权限限制
- Jsp和PHP共用80端口整合Apache和Tomcat(访问时无需加端口号)
- Tomcat服务器 安全设置第1/3页
- tomcat 6.0.20在一个机器上安装多个服务的方法
- Tomcat 5.5 数据库连接池配置
- Tomcat内存溢出分析及解决方法
- apache tomcat 一个网站多域名的实现方法