解决浏览器不兼容websocket
1.新建web project。
2.找到tomcat 7.0 lib 下的 catalina.jar,tomcat-coyote.jar添加到项目中.
3.如下是我的目录结构
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <display-name>Archetype Created Web Application</display-name>
- <servlet>
- <servlet-name>serverSocket</servlet-name>
- <servlet-class>com.sun.websocket.server.ServerSocket</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>serverSocket</servlet-name>
- <url-pattern>/serverSocket</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
ServerSocket.java的源码.
- package com.sun.websocket.server;
- import java.io.IOException;
- import java.nio.ByteBuffer;
- import java.nio.CharBuffer;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.UUID;
- import java.util.concurrent.ConcurrentHashMap;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.catalina.websocket.MessageInbound;
- import org.apache.catalina.websocket.StreamInbound;
- import org.apache.catalina.websocket.WebSocketServlet;
- import org.apache.catalina.websocket.WsOutbound;
- public class ServerSocket extends WebSocketServlet {
- private static final long serialVersionUID = -4853540828121130946L;
- private static Map< String , MyMessageInbound> mmiList = new ConcurrentHashMap< String , MyMessageInbound >();
- private String message_to ;
- private String message_me ;
- @Override
- protected StreamInbound createWebSocketInbound(String arg0, HttpServletRequest request) {
- message_me = request.getParameter( "message_me" );
- message_to = request.getParameter( "message_to" );
- return new MyMessageInbound();
- }
- private class MyMessageInbound extends MessageInbound {
- WsOutbound myoutbound;
- private String me = message_me ;
- private String to = message_to ;
- @Override
- public void onOpen(WsOutbound outbound) {
- try {
- System.out.println("Open " + me + " to " + to);
- this.myoutbound = outbound;
- mmiList.put( me , this );
- outbound.writeTextMessage(CharBuffer.wrap("Hello!"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onTextMessage(CharBuffer cb) throws IOException {
- System.out.println("Accept Message : " + cb);
- for ( String mmib : mmiList.keySet() ) {
- if ( !to.equals(mmib) )
- continue;
- try
- {
- CharBuffer buffer = CharBuffer.wrap(cb);
- mmiList.get(mmib).myoutbound.writeTextMessage(buffer);
- mmiList.get(mmib).myoutbound.flush();
- }
- catch (Exception e) {
- continue;
- }
- break;
- }
- }
- @Override
- public void onClose(int status) {
- if( status == 1002 || status == 1000)
- {
- System.out.println("Close " + me + " to " + to);
- mmiList.remove(this);
- }
- }
- @Override
- public void onBinaryMessage(ByteBuffer bb) throws IOException {
- }
- }
- }
接下来编写index.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">
- <script type="text/javascript" src="scripts/swfobject.js"></script>
- <script type="text/javascript" src="scripts/jquery.js"></script>
- <script type="text/javascript" src="scripts/web_socket.js"></script>
- <script type="text/javascript" src="scripts/jquery.WebSocket.js"></script>
- <%
- String message_to = request.getParameter( "message_to" );
- String message_me = request.getParameter( "message_me" );
- request.setAttribute( "message_to" , message_to );
- request.setAttribute( "message_me" , message_me );
- %>
- <script>
- $(function ()
- {
- window.onbeforeunload = onbeforeunload_handler;
- window.onunload = onunload_handler;
- function onbeforeunload_handler(){
- //ws.close();
- return warning;
- }
- function onunload_handler()
- {
- //alert(1);
- ws = null;
- }
- });
- var message_to = "${message_to}";
- var message_me = "${message_me}";
- //var ws = new WebSocket("ws://192.168.202.56:8080/websocket_msg/serverSocket?message_to="+message_to+"&message_me="+message_me);
- var url = "websocket_msg/serverSocket?message_to="+message_to+"&message_me="+message_me;
- var ws = new $.websocket({
- protocol : "websocket_msg/serverSocket?message_to="+message_to+"&message_me="+message_me,
- domain : "192.168.1.120",
- port : "8080",
- onOpen:function(event){
- showMessage("已成功登录");
- },
- onError:function(event){
- alert("error:"+ event)
- },
- onMessage:function(result){
- receiveMessage(result);
- },
- onClose:function(event){
- ws = null;
- }
- });
- function send(){
- if(!ws){
- alert("已经断开聊天室");
- return;
- }
- var msg=$.trim($("#msg").val());
- if(msg==""){return;}
- ws.send(msg);
- $("#messageInput").val("").focus();;
- }
- function receiveMessage(result){
- showMessage(result);
- }
- function showMessage(msg){
- document.getElementById("chatlog").textContent += msg + "\n";
- }
- </script>
- </head>
- <body>
- <body>
- <textarea id="chatlog" readonly style="width:500px;height:500px;"></textarea><br/>
- <input id="msg" type="text" />
- <button type="submit" id="sendButton" onClick="send()">Send!</button>
- <button type="submit" id="sendButton" onClick="closeConnect()">End</button>
- </body>
- </body>
- </html>
编写完成后,访问index.jsp时需要URL给出两个参数。一个代表发送者,一个代表接收者。
- 例如 ?message_to=1&message_me=2"
解决浏览器不兼容websocket的更多相关文章
- jsp关闭或刷新浏览器(解决浏览器不兼容),请求后台onbeforeunload、onunload
jsp关闭或刷新浏览器(解决浏览器不兼容),请求后台 onbeforeunload.onunload 1.看代码: function test(e) { var json = "退出,清理 ...
- html5--4-3 source元素-解决浏览器的兼容
html5--4-3 source元素-解决浏览器的兼容 学习要点 掌握source元素的用法 source元素-解决浏览器额兼容 HTML5 中新增的元素 video和audio元素的子元素,可指定 ...
- js复制当前url地址解决浏览器兼容
1.网上搜索的js复制链接代码,好像只能支持ie浏览器,其他浏览器不支持, 案例: var url=12; if(window.clipboardData){ wi ...
- IE内嵌google chrome frame解决浏览器兼容问题
IE内嵌google chrome frame解决浏览器兼容问题 http://www.cnblogs.com/xwdreamer/archive/2013/12/17/3477776.html 参 ...
- 教你一招解决浏览器兼容问题(PostCSS的使用)
我们在处理网页的时候,往往会遇到兼容性的问题.在这个问题上分为两个大的方向:屏幕自适应&浏览器兼容.而屏幕自使用的方法有许多,包括框架之类的,但是浏览器的兼容却没有一个号的框架.在我们日常处理 ...
- 一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10
行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10 2012-04-25 16:29:04| 分类: 学习 |字号 订阅 在网站开发中不免因为各种兼容问题苦恼,针对兼容问题,其实IE ...
- CSS Hack解决浏览器IE部分属性兼容性问题
1.Css Hack 不同厂商的流览器或某浏览器的不同版本(如IE6-IE11,Firefox/Safari/Opera/Chrome等),对CSS的支持.解析不一样,导致在不同浏览器的环境中呈现出不 ...
- 一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10 http://www.jb51.net/css/383986.html
在网站开发中不免因为各种兼容问题苦恼,针对兼容问题,其实IE给出了解决方案Google也给出了解决方案百度也应用了这种方案去解决IE的兼容问题 百度源代码如下 复制代码 代码如下: <!Do ...
- 复制到剪贴板的JS实现--ZeroClipboard (兼解决IE下兼容问题)
复制到剪贴板的JS实现--ZeroClipboard (兼解决IE下兼容问题) 相信绝大多数人都遇到过这样的功能实现,“复制”或者“复制到剪贴板”这样的功能.但是由于各大浏览器的实现方案不一样,导致几 ...
随机推荐
- POJ 3256 DFS水题
枚举点 每次都搜一遍 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> ...
- [ Linux ] 釋放記憶體指令(cache) - 轉載
1. [Linux]釋放記憶體指令(cache) http://jeffreyy.pixnet.net/blog/post/84333764-%E3%80%90linux%E3%80%91%E9%87 ...
- Linux下查看进程IO工具iopp
Linux下的IO检测工具最常用的是iostat,不过iostat只能查看到总的IO情况.如果要细看具体那一个程序点用的IO较高,可以使用iotop .不过iotop对内核版本和Python版本有要求 ...
- jQuery中四种事件监听的区别
原文链接:点我 我们知道jquery提供了四种事件监听方式,分别是bind.live.delegate.on,下面就分别对这四种事件监听方式分析. 已知有4个列表元素: 列表元素1 列表元素2 列表元 ...
- 使用form-create动态生成vue组件
使用form-create动态生成vue自定义组件和嵌套表单组件 [github] | [说明文档] 示例 let rule = [ { type:'row', children:[ { type:' ...
- 紫书 例题 9-2 UVa 437 ( DAG的动态规划)
很明显可以根据放不放建边,然后最一遍最长路即是答案 DAG上的动态规划就是根据题目中的二元关系来建一个 DAG,然后跑一遍最长路和最短路就是答案,可以用记忆化搜索的方式来实现 细节:(1)注意初始化数 ...
- 发送邮件被退回,提示: Helo command rejected: Invalid name 错误
我自己配置的 postfix + dovecot server, 配置了outlook 后, 相同的账号. 在有的电脑上能收发成功, 在有的电脑上发送邮件就出现退信.提示 Helo command r ...
- 【HDOJ 5384】Danganronpa
[HDOJ 5384]Danganronpa AC自己主动机. .. 当时感觉用字典树 标神也往自己主动机想来着..手太生加上时间紧迫也没敲--回来一看题解什么AB同一时候建自己主动机...顿时愣了 ...
- iOS Core Animation具体解释(四)AutoLayout中的动画
原创blog.转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的iOS SDK具体解释专栏 http://blog.csdn.net/column/details/huang ...
- Python: PS 图像调整--对比度调整
本文用 Python 实现 PS 里的图像调整–对比度调整.具体的算法原理如下: (1).nRGB = RGB + (RGB - Threshold) * Contrast / 255 公式中,nRG ...