⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 periodicclientping.java

📁 是一款国外的网络游戏平台的源码*不是类似浩方那种虚拟局域网技术
💻 JAVA
字号:
/*
 * Created on Jan 26, 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.GTADS.server;

import java.io.IOException;
import java.net.Socket;
import java.util.Date;
import java.util.Vector;

import org.GTADS.messenger.MessageAdapter;
import org.GTADS.protocol.MetaData;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class PeriodicClientPing extends Thread{
	public static PeriodicClientPing instance;
	
	public static PeriodicClientPing getInstance(){
		if (instance == null){
			instance = new PeriodicClientPing();
		}
		return instance;
	}
	
	public static void clearInstance(){
		if (instance != null){
			instance = null;
		}
	}
	
	PeriodicClientPing(){
		this.setName(this.getClass().getName());
		this.setPriority(10);
		start();
	}
	
	public void run(){
		try {
			while (true) {
				Thread.sleep(ServerConfig.getInstance().userPingInterval() * 1000);
				sendPingToAllUsers();
			}
		} catch (InterruptedException ie){
			return;
		}
	}
	
	private void sendPingToAllUsers(){
		Vector collectionOfSockets = new Vector(ServerCacheHandler.userSocketCache.values());
		MetaData pingHeader = new MetaData(MetaData.FROM_SERVER, MetaData.GENERIC, "All");
		int i = 0;
		
		try {
			for (i = 0; i < collectionOfSockets.size(); i++){
				Socket s = (Socket)collectionOfSockets.elementAt(i);
				MessageAdapter.sendData(null,s,pingHeader,"PING");
				String userName = (String)ServerCacheHandler.socketUserCache.get(s);
				ServerCacheHandler.userPingTimeCache.put(userName,new UserPing());
			}
		} catch (IOException ie) {
			//DSChatServer.getInstance().killConnection((Socket)collectionOfSockets.elementAt(i),serverInstance);	
		}
	}
	
	public class UserPing {
		private Long timeStamp;
		private Long interval;
		
		public UserPing(){
			Date d = new Date();
			timeStamp = new Long(d.getTime());
		}
		
		public void setInterval(Long interval){
			this.interval = interval;
		}
		
		public Long getPingTime(){
			Long pingInterval = null;
			
			if (interval != null){
				pingInterval = new Long(interval.longValue() - timeStamp.longValue());
			}
			
			return pingInterval;
		}
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -