resin 使用websocket

核心思路

1. 编写一个socketServlet,它extends resin开发包中的httpServlet

2. 编写一个scoketListener,它implements resin包中的WebSocketListener

3. 将socketServlet配置在resin配置给resin,比如在web.xml中配置

解释

注意,本实例的resin版本为:resin-4.0.53

1. socketServlet的作用是在听到指定地址的请求后,将请求升级为websocket协议。因此在该servlet的service方法中将协议升价socket即可。该servelt与普通的servlet相比没什么区别,仅仅是service中做了不一样的事情。

2. scoketListener 实现的resin包下WebSocketListener类,类全路径:

              com.caucho.websocket.WebSocketListener

   在该类中有诸多socket表现行为的接口,实现它们即可完成socket通信,接口列出:

void onStart(WebSocketContext var1) throws IOException;

void onReadBinary(WebSocketContext var1, InputStream var2) throws IOException;

void onReadText(WebSocketContext var1, Reader var2) throws IOException;

void onClose(WebSocketContext var1) throws IOException;

void onDisconnect(WebSocketContext var1) throws IOException;

void onTimeout(WebSocketContext var1) throws IOException;

3. scoketListener的发送是使用 WebSocketContext 类实现的,类似于tomcat websocket中的session。

 

示例

注意:代码仅供参考,重要的是逻辑思路

1. ResinSocketServlet


import com.caucho.websocket.WebSocketContext;
import com.caucho.websocket.WebSocketServletRequest;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Principal;

public class ResinSocketServlet extends HttpServlet {

    public ResinSocketServlet() {
        System.out.println("ResinSocketServlet init...");
    }

    public void service(HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException
    {

        Principal principal = AuthHandler.determineUser(req);
        if (principal == null) {
            res.sendError(401);
        } else {
            TdtWebSocketListener listener = new TdtWebSocketListener();
            WebSocketServletRequest wsReq = (WebSocketServletRequest) req;
            WebSocketContext webSocketContext = wsReq.startWebSocket(listener);
            UserStorage.setPrincipal(webSocketContext, principal);
        }
    }

}

2. MyWebSocketListener

import com.caucho.websocket.WebSocketContext;
import com.caucho.websocket.WebSocketListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.security.Principal;

public class MyWebSocketListener implements WebSocketListener {
    private Logger logger = LoggerFactory.getLogger(MyWebSocketListener.class);

    @Override
    public void onStart(WebSocketContext webSocketContext) throws IOException {
        logger.info("+++++++++++++++++++++ MyWebSocketListener onStart +++++++++++++++++++++ ");
    }

    @Override
    public void onReadBinary(WebSocketContext webSocketContext, InputStream inputStream) throws IOException {
        logger.info("+++++++++++++++++++++ MyWebSocketListener onReadBinary+++++++++++++++++++++ ");
    }

    @Override
    public void onReadText(WebSocketContext webSocketContext, Reader reader) throws IOException {
        logger.info("+++++++++++++++++++++ MyWebSocketListener onReadText +++++++++++++++++++++ ");
        Principal principal = UserStorage.getPrincipal(webSocketContext);  //--这里可以先校验用户是否登录
        if (principal == null) {//校验用户是否登录
            webSocketContext.close();
        } else {
            StringBuffer msg = new StringBuffer();
            int ch2;
            while ((ch2 = reader.read()) > 0) {
                msg.append((char) ch2);
            }
			System.out.println("收到信息: "+msg.toString());
			
			try {//写回信息
				PrintWriter writer = webSocketContext.startTextMessage();
				writer.print(msg);
				writer.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			webSocketContext.close();
        }

    }

    @Override
    public void onClose(WebSocketContext webSocketContext) throws IOException {
        logger.info("+++++++++++++++++++++ MyWebSocketListener onClose +++++++++++++++++++++ ");
        clearUser(webSocketContext);
    }

    @Override
    public void onDisconnect(WebSocketContext webSocketContext) throws IOException {
        logger.info("+++++++++++++++++++++ MyWebSocketListener onDisconnect +++++++++++++++++++++ ");
        clearUser(webSocketContext);
    }

    @Override
    public void onTimeout(WebSocketContext webSocketContext) throws IOException {
        logger.info(" +++++++++++++++++++++  MyWebSocketListener onTimeout +++++++++++++++++++++ ");
        clearUser(webSocketContext);
    }

    private void clearUser(WebSocketContext webSocketContext) {
        UserStorage.removePrincipal(webSocketContext);
    }
}

3. 配置

在xml里配置即可,很简单,这里不赘述

你可能感兴趣的:(resin,websocket)