tio-websocket 0.0.4 发布,和 tio 底层 API 无缝对接

tio-websocket是基于tio实现的websocket服务器,使用方式极其简单

本次修改点

1、t-io版本升级到2.0.2.v20171129-RELEASE

2、tio-http版本升级到0.0.4-tio-http

3、简单优化一下demo程序

最新pom坐标

<dependency>
	<groupId>org.t-io</groupId>
	<artifactId>tio-websocket-server</artifactId>
	<version>0.0.4-tio-websocket</version>
</dependency>

如何使用

1、实现IWsMsgHandler

package org.tio.websocket.server.demo1;

import java.nio.ByteBuffer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.Aio;
import org.tio.core.ChannelContext;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.websocket.common.WsRequest;
import org.tio.websocket.server.handler.IWsMsgHandler;

/**
 * @author tanyaowu
 * 2017年6月28日 下午5:32:38
 */
public class WsDemoMsgHandler implements IWsMsgHandler 
	private static Logger log = LoggerFactory.getLogger(WsDemoMsgHandler.class);

	/**
	 * 握手时走这个方法,业务可以在这里获取cookie,request参数等
	 */
	@Override
	public HttpResponse handshake(HttpRequest request, HttpResponse httpResponse, ChannelContext channelContext) throws Exception 
		return httpResponse;
	

	/**
	 * 字节消息(binaryType = arraybuffer)过来后会走这个方法
	 */
	@Override
	public Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception 
		String ss = new String(bytes, "utf-8");
		log.info("收到byte消息:,{}", bytes, ss);

		//		byte[] bs1 = "收到byte消息".getBytes("utf-8");
		ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
		buffer.put(bytes);

		return buffer;
	}

	/**
	 * 当客户端发close flag时,会走这个方法
	 */
	@Override
	public Object onClose(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception 
		Aio.remove(channelContext, "receive close flag");
		return null;
	

	/**
	 * 字符消息(binaryType = blob)过来后会走这个方法
	 */
	@Override
	public Object onText(WsRequest wsRequest, String text, ChannelContext channelContext) throws Exception 
		return "收到text消息:" + text;
	
}

2、启动

package org.tio.websocket.server.demo1;

import java.io.IOException;

import org.tio.server.ServerGroupContext;
import org.tio.websocket.server.WsServerStarter;

/**
 * @author tanyaowu
 * 2017年6月28日 下午5:34:04
 */
public class WsDemoStarter 
	/**
	 * @param args
	 * @author tanyaowu
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException 
		WsDemoStarter appStarter = new WsDemoStarter(9321, new WsDemoMsgHandler());
		appStarter.start();
	

	private ServerGroupContext serverGroupContext;
	private WsServerStarter wsServerStarter;

	/**
	 *
	 * @author tanyaowu
	 */
	public WsDemoStarter(int port, WsDemoMsgHandler wsMsgHandler) throws IOException 
		wsServerStarter = new WsServerStarter(port, wsMsgHandler);
		serverGroupContext = wsServerStarter.getServerGroupContext();
	

	/**
	 * @return the serverGroupContext
	 */
	public ServerGroupContext getServerGroupContext() 
		return serverGroupContext;
	

	public WsServerStarter getWsServerStarter() 
		return wsServerStarter;
	

	public void start() throws IOException 
		wsServerStarter.start();
	
}

Original post:  

tio-websocket 0.0.4 发布,和 tio 底层 API 无缝对接