基於netty的聊天室

chenshiying007發表於2018-06-21

基於netty的聊天室

更多幹貨

部分程式碼

ctoedu
ctoedu
ctoedu

netty4 部分程式碼

package com.cn.common.core.session;
/**
 * 會話抽象介面
 *
 *
 */
public interface Session {

	/**
	 * 會話繫結物件
	 * @return
	 */
	Object getAttachment();

	/**
	 * 繫結物件
	 * @return
	 */
	void setAttachment(Object attachment);

	/**
	 * 移除繫結物件
	 * @return
	 */
	void removeAttachment();

	/**
	 * 向會話中寫入訊息
	 * @param message
	 */
	void write(Object message);

	/**
	 * 判斷會話是否在連線中
	 * @return
	 */
	boolean isConnected();

	/**
	 * 關閉
	 * @return
	 */
	void close();
}

SessionImpl

package com.cn.common.core.session;

import io.netty.channel.Channel;
import io.netty.util.AttributeKey;

/**
 * 會話封裝類
 *
 *
 */
public class SessionImpl implements Session {

	/**
	 * 繫結物件key
	 */
	public static AttributeKey<Object> ATTACHMENT_KEY  = AttributeKey.valueOf("ATTACHMENT_KEY");

	/**
	 * 實際會話物件
	 */
	private Channel channel;


	public SessionImpl(Channel channel) {
		this.channel = channel;
	}

	@Override
	public Object getAttachment() {
		return channel.attr(ATTACHMENT_KEY).get();
	}

	@Override
	public void setAttachment(Object attachment) {
		channel.attr(ATTACHMENT_KEY).set(attachment);
	}

	@Override
	public void removeAttachment() {
		channel.attr(ATTACHMENT_KEY).remove();
	}

	@Override
	public void write(Object message) {
		channel.writeAndFlush(message);
	}

	@Override
	public boolean isConnected() {
		return channel.isActive();
	}

	@Override
	public void close() {
		channel.close();
	}



}

SessionManager

package com.cn.common.core.session;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import com.cn.common.core.model.Response;
import com.cn.common.core.serial.Serializer;
import com.cn.common.core.session.Session;
import com.google.protobuf.GeneratedMessage;
/**
 * 會話管理者
 *
 *
 */
public class SessionManager {

	/**
	 * 線上會話
	 */
	private static final ConcurrentHashMap<Long, Session> onlineSessions = new ConcurrentHashMap<>();

	/**
	 * 加入
	 * @param playerId
	 * @param channel
	 * @return
	 */
	public static boolean putSession(long playerId, Session session){
		if(!onlineSessions.containsKey(playerId)){
			boolean success = onlineSessions.putIfAbsent(playerId, session)== null? true : false;
			return success;
		}
		return false;
	}

	/**
	 * 移除
	 * @param playerId
	 */
	public static Session removeSession(long playerId){
		return onlineSessions.remove(playerId);
	}

	/**
	 * 傳送訊息[自定義協議]
	 * @param <T>
	 * @param playerId
	 * @param message
	 */
	public static <T extends Serializer> void sendMessage(long playerId, short module, short cmd, T message){
		Session session = onlineSessions.get(playerId);
		if (session != null && session.isConnected()) {
			Response response = new Response(module, cmd, message.getBytes());
			session.write(response);
		}
	}

	/**
	 * 傳送訊息[protoBuf協議]
	 * @param <T>
	 * @param playerId
	 * @param message
	 */
	public static <T extends GeneratedMessage> void sendMessage(long playerId, short module, short cmd, T message){
		Session session = onlineSessions.get(playerId);
		if (session != null && session.isConnected()) {
			Response response = new Response(module, cmd, message.toByteArray());
			session.write(response);
		}
	}

	/**
	 * 是否線上
	 * @param playerId
	 * @return
	 */
	public static boolean isOnlinePlayer(long playerId){
		return onlineSessions.containsKey(playerId);
	}

	/**
	 * 獲取所有線上玩家
	 * @return
	 */
	public static Set<Long> getOnlinePlayers() {
		return Collections.unmodifiableSet(onlineSessions.keySet());
	}
}

相關文章