本文整理汇总了Java中de.gymdon.inf1315.game.packet.PacketHeartbeat类的典型用法代码示例。如果您正苦于以下问题:Java PacketHeartbeat类的具体用法?Java PacketHeartbeat怎么用?Java PacketHeartbeat使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PacketHeartbeat类属于de.gymdon.inf1315.game.packet包,在下文中一共展示了PacketHeartbeat类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: update
import de.gymdon.inf1315.game.packet.PacketHeartbeat; //导入依赖的package包/类
public void update() {
if (server.clientList.isEmpty())
return;
for (Iterator<Remote> i = server.clientList.iterator(); i.hasNext();) {
Remote r = i.next();
try {
DataInputStream din = r.getInputStream();
if (din.available() >= 2) {
short id = din.readShort();
Packet p = Packet.newPacket(id, r);
if (p != null)
p.handlePacket();
else
r.kick("Invalid Packet");
}
if (r.getSocket().isClosed())
r.leave("Socket closed");
long now = System.currentTimeMillis();
if (now - r.getLastPacketTime() >= 2000) {
PacketHeartbeat heartbeat = new PacketHeartbeat(r);
heartbeat.response = false;
byte[] bytes = new byte[43];
new Random().nextBytes(bytes);
heartbeat.payload = bytes;
heartbeat.send();
}
} catch (Exception e) {
r.leave(e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage());
}
if (r.left())
i.remove();
}
}
示例2: processPackets
import de.gymdon.inf1315.game.packet.PacketHeartbeat; //导入依赖的package包/类
public void processPackets() {
if(left())
return;
try {
DataInputStream din = getInputStream();
if (din.available() >= 2) {
short id = din.readShort();
Packet p = Packet.newPacket(id, this);
if (p != null)
p.handlePacket();
else
kick("Invalid Packet");
}
if (getSocket().isClosed())
leave("Socket closed");
long now = System.currentTimeMillis();
if(lastPacket == 0)
lastPacket = System.currentTimeMillis();
if (now - this.getLastPacketTime() >= 2100) {
PacketHeartbeat heartbeat = new PacketHeartbeat(this);
heartbeat.response = false;
byte[] bytes = new byte[43];
new Random().nextBytes(bytes);
heartbeat.payload = bytes;
heartbeat.send();
}
} catch (Exception e) {
leave(e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage());
}
}