The Server class extends thread and has a run method that calls onReceive().
public void onReceive(SocketChannel client, ByteBuffer message) {
int type = message.getInt();
switch(type) {
//other case statements...
case Constants.MSG_CHAT:
// testing
manager.sendUniversalMessage(client, message); // never get this
// and
//rawSend(client, message); // when uncommented, never get this either
break;
default:
break;
}
}
The manager is the ServerManager class, which extends thread and has the following
// send packet along
public void sendUniversalMessage(SocketChannel client, ByteBuffer b) {
try
{
// send client message
synchronized(b) {
b.rewind(); // so the MSG_CHAT marker is the beginning
}
ServerPlayer pl;
for (Enumeration e = players.elements() ; e.hasMoreElements() ;) {
pl = (ServerPlayer)e.nextElement();
pl.addDataToQueue(b); // adds to a LinkedList on player
System.out.println("Sending chat to: " + pl.getAlias()); // I see this watching server
pl = null;
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
then later on in the run…
public void run {
// many other odds and ends like tick the serverplayership for movement and build
// one giant update packet of all players and positions and add to player's queue..t
// then the queues are called upon after setting a ServerPlayer object
for (Enumeration e = players.elements() ; e.hasMoreElements() ;) {
pl = (ServerPlayer)e.nextElement();
if(pl.getClient().isConnected() && pl.isReadyForUpdate()) {
pl.addDataToQueue(udpacket);
}
pl = null;
}
// then
// send the packets from the players queue
for (Enumeration e = players.elements() ; e.hasMoreElements() ;) {
pl = (ServerPlayer)e.nextElement();
if(pl.getClient().isConnected()) {
pl.send();
}
pl = null;
}
}
Now the players send method…all of the data makes it to the queue, I always get login and logout data
and all movement pakcets and the profile packet with text and numbers for the ‘character sheet’ BUT no chat messages ever make it to the player
and the rare ones that do…its all gibberish and garbage characters
public synchronized void addDataToQueue(ByteBuffer b) {
if(dataQueue == null){
dataQueue = new LinkedList<ByteBuffer>();
}
dataQueue.add(b);
}
public synchronized void send() {
for(int i=0;i<dataQueue.size();i++) {
ByteBuffer b = dataQueue.poll();
if(b == null)
return;
try {
if(b.getInt() == Constants.MSG_CHAT) {
System.out.println("Sending chat to player"); // i see this on server
}
b.rewind();
client.write(b);
}
catch(Exception ioe) {
}
}
return;
}
Everything except chat comes to the players receive method!
the players client class extends thread and has an onReceive as well and in the switch statement I have the following among other thins
case Constants.MSG_CHAT:
// get channel id then create mesage
// this is the text of the message
line = jenn_toString(message);
System.out.println("Got chat from server: " + line); // never see this
Manager.getHUD().getChatPanel().addMsg(line);
break;