mina+webSocket 搭建兼容各版本浏览器无刷新评论实例

本文介绍了如何在项目框架受限的情况下,利用mina和WebSocket技术搭建一个兼容多版本浏览器的无刷新评论系统。在实现过程中,解决了协议、转码和兼容性问题。针对协议和转码,参考了DIRMINA-907中的WebSocketFilter解决方案;为了解决兼容性,采用SWF并使用FlashPolicyServer处理<policy-file-request/>请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目框架是别人定的,我也想使用nodeJS或者tomcat 7自带的WebSocketServlet,无奈。。

过程中碰到几个比较烦人的问题:

1、协议

2、转码

3、兼容性

对于协议和转码,我是用https://issues.apache.org/jira/browse/DIRMINA-907里面的WebSocketFilter;

对于兼容性,我是用swf代替,这里还会有个swf与java通信的问题,socket会收到<policy-file-request/>,我这里使用http://my.oschina.net/noahxiao/blog/71611的FlashPolicyServer这个类处理

下面粘部分代码实现:

public class WebSocketServer {
	public static final int PORT = 10000;

	public static void main(String[] args) {

		WebSocketIoHandler handler = new WebSocketIoHandler();

		NioSocketAcceptor acceptor = new NioSocketAcceptor();
	        
		acceptor.getFilterChain().addLast("threadPool", new ExecutorFilter(Executors.newCachedThreadPool()));
		acceptor.getFilterChain().addLast("protocol", new ProtocolCodecFilter(new WebSocketCodecFactory()));
		acceptor.setHandler(handler);

		try {
			acceptor.bind(new InetSocketAddress(PORT));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

public class WebSocketIoHandler extends IoHandlerAdapter {
	
<span style="white-space:pre">	</span>public static final String INDEX_KEY = WebSocketIoHandler.class.getName() + ".INDEX";
    	
    Map<Integer, List<IoSession>> videoIoSession = new HashMap<Integer, List<IoSession>>();
    
    public void messageReceived(IoSession session, Object message) throws Exception {
    	IoBuffer buffer = (IoBuffer)message;
    	byte[] b = new byte[buffer.limit()];  
    	buffer.get(b);
    	
    	String code = new String(b, "utf-8");
//    	System.out.println(code);
    	
    	if(code == null || code.equals("")){
    		return ;
    	}
    	
    	
    	//105100 --->videoId
    	if(code.startsWith("videoid")){
    		int videoId = 0;
    		try {
    			String id = code.substring(7);
//        		System.out.println("------------videoid"+id);
        		videoId = Integer.valueOf(id);
			} catch (Exception e) {
				return ;
			}
    		if(videoId <= 0){
    			
    			return ;
    		}
    		ioSessionIsIn
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值