首页 > 代码库 > 基于tomcat7 的websocket

基于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 {


}


}


基于tomcat7 的websocket