首页 > 代码库 > 介绍MyEclipse集成Tomcat7,并开发WebSocket的全过程
介绍MyEclipse集成Tomcat7,并开发WebSocket的全过程
1.首先使MyEclipse8.6集成Tomcat7
注意配置参数
注意jar包
主机JDK版本
2.开发WebSocket
导入websocket-api.jar
开发java文件
package com.chen.websocket;import java.io.IOException;import java.util.concurrent.CopyOnWriteArraySet; import javax.websocket.OnClose;import javax.websocket.OnError;import javax.websocket.OnMessage;import javax.websocket.OnOpen;import javax.websocket.Session;import javax.websocket.server.ServerEndpoint; @ServerEndpoint("/websocket")public class MyWebSocket { private static int onlineCount = 0; private static CopyOnWriteArraySet<MyWebSocket> webSocketSet = new CopyOnWriteArraySet<MyWebSocket>(); private Session session; @OnOpen public void onOpen(Session session){ this.session = session; webSocketSet.add(this); addOnlineCount(); System.out.println("aaa?" + getOnlineCount()); } @OnClose public void onClose(){ webSocketSet.remove(this); subOnlineCount(); System.out.println("bbbb?" + getOnlineCount()); } @OnMessage public void onMessage(String message, Session session) { System.out.println("ttt" + message); for(MyWebSocket item: webSocketSet){ try { item.sendMessage(message); } catch (IOException e) { e.printStackTrace(); continue; } } } @OnError public void one rror(Session session, Throwable error){ System.out.println("yy"); error.printStackTrace(); } public void sendMessage(String message) throws IOException{ this.session.getBasicRemote().sendText(message); //this.session.getAsyncRemote().sendText(message); } public static synchronized int getOnlineCount() { return onlineCount; } public static synchronized void addOnlineCount() { MyWebSocket.onlineCount++; } public static synchronized void subOnlineCount() { MyWebSocket.onlineCount--; }}
开发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><html> <head> <base href="http://www.mamicode.com/"> <title>My WebSocket</title> </head> <body> Welcome<br/> <input id="text" type="text" /><button onclick="send()">Send</button> <button onclick="closeWebSocket()">Close</button> <div id="message"> </div> </body> <script type="text/javascript"> var websocket = null; //判断当前浏览器是否支持WebSocket if(‘WebSocket‘ in window){ websocket = new WebSocket("ws://localhost:8080/MyWebSocket/websocket"); } else{ alert(‘Not support websocket‘) } //连接发生错误的回调方法 websocket.onerror = function(){ setMessageInnerHTML("error"); }; //连接成功建立的回调方法 websocket.onopen = function(event){ setMessageInnerHTML("open"); } //接收到消息的回调方法 websocket.onmessage = function(){ setMessageInnerHTML(event.data); } //连接关闭的回调方法 websocket.onclose = function(){ setMessageInnerHTML("close"); } //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。 window.onbeforeunload = function(){ websocket.close(); } //将消息显示在网页上 function setMessageInnerHTML(innerHTML){ document.getElementById(‘message‘).innerHTML += innerHTML + ‘<br/>‘; } //关闭连接 function closeWebSocket(){ websocket.close(); } //发送消息 function send(){ var message = document.getElementById(‘text‘).value; websocket.send(message); } </script></html>
3.部署
部署完之后需要在部署文件下删除websocket-api.jar,因为tomcat7已经有
4.运行项目
介绍MyEclipse集成Tomcat7,并开发WebSocket的全过程
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。