Skip to content

Instantly share code, notes, and snippets.

@yaronv
Created January 24, 2015 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaronv/1f37621c42cfea947cdc to your computer and use it in GitHub Desktop.
Save yaronv/1f37621c42cfea947cdc to your computer and use it in GitHub Desktop.
package dataObjects;
import java.io.IOException;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.Date;
import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.WsOutbound;
import utils.AccountManager;
import utils.MessagesService;
public class AccountConnection extends MessageInbound implements Serializable {
private static final long serialVersionUID = 1L;
private final String uniqueId;
/**
* class constructor
*/
public AccountConnection(String uniqueId) {
this.uniqueId = uniqueId;
}
@Override
protected void onOpen(WsOutbound outbound) {
System.out.println(new Date() + " account " + this.uniqueId + " is now online.");
}
@Override
protected void onClose(int status) {
// remove from accounts
AccountManager.instance().removeOnlineAccount(this.uniqueId);
System.out.println(new Date() + " account " + this.uniqueId + " is now offline.");
}
@Override
protected void onBinaryMessage(ByteBuffer message) throws IOException {
throw new UnsupportedOperationException("Binary message not supported.");
}
@Override
protected void onTextMessage(CharBuffer message) throws IOException {
MessagesService.instance().parseClientMessage(message.toString());
}
/**
* send a message to the account
* @param message - the message to send
* @return true if sent, false if failed
*/
public boolean sendMessage(String message) {
CharBuffer buffer = CharBuffer.wrap(message);
try {
this.getWsOutbound().writeTextMessage(buffer);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment