基于tomcat7 的websocket

基于tomcat7.0使用websocket

jdk1.7

tomcat7.0

创建WebSocketServlet

package com.biz.action.servlet;


import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServletRequest;


import org.apache.catalina.websocket.StreamInbound;

import org.apache.catalina.websocket.WebSocketServlet;


import com.biz.websocket.LogMessageInbound;

@WebServlet("/log")

public class LogSocketServlet extends WebSocketServlet {

/**

*/

private static final long serialVersionUID = 5050900528174933757L;


public LogSocketServlet(){

System.out.println("logSocket create");

}


@Override

protected StreamInbound createWebSocketInbound(String arg0,

HttpServletRequest request) {

                        return new LogMessageInbound();

}


}

创建MessageInbound


package com.biz.websocket;


import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.CharBuffer;


import org.apache.catalina.websocket.MessageInbound;

import org.apache.catalina.websocket.WsOutbound;


import redis.clients.jedis.Jedis;


import com.biz.thread.LogThread;

import com.core.redis.MyRedisListener;

import com.core.redis.handler.MyHandler;


public class LogMessageInbound extends MessageInbound{


@Override

protected void onOpen(WsOutbound outbound) {

System.out.println("onOpen start");


System.out.println("onOpen end");

}

@Override

protected void onClose(int state) {

System.out.println("state="+state);

}

@Override

protected void onBinaryMessage(ByteBuffer arg0) throws IOException {


}


@Override

protected void onTextMessage(CharBuffer arg0) throws IOException {


}


}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。