periodicclientping.java
来自「是一款国外的网络游戏平台的源码*不是类似浩方那种虚拟局域网技术」· Java 代码 · 共 98 行
JAVA
98 行
/*
* 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 + =
减小字号Ctrl + -
显示快捷键?