package com.web;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.WsOutbound;
public class MyMessageInbound extends MessageInbound {
private int userIdName = 0;
public int getUserIdName() {
return userIdName;
}
protected void onOpen(WsOutbound outbound) {
super.onOpen(outbound);
userIdName = outbound.hashCode();
EchoServlet.getSocketList().add(this);
}
protected void onClose(int status) {
EchoServlet.getSocketList().remove(this);
super.onClose(status);
}
// 有二进制消息数据到达,比如音频等文件传输
@Override
protected void onBinaryMessage(ByteBuffer buffer) throws IOException {
}
@Override
protected void onTextMessage(CharBuffer buffer) throws IOException {
String msgOriginal = buffer.toString();
System.out.println("msgOriginal:::"+msgOriginal);
int startIndex = msgOriginal.indexOf("[email protected]#$%");
String nikeName = msgOriginal.substring(0, startIndex);
String textMsg = msgOriginal.substring(startIndex + 5);
// 将字符数组包装到缓冲区中
// 给定的字符数组将支持新缓冲区;即缓冲区修改将导致数组修改,反之亦然
String countMsg = EchoServlet.getSocketList().size() + "人同时在线";
System.out.println("同时在线人数:::"+countMsg);
String msg1 = "
String msg2 = "
System.out.println(EchoServlet.getSocketList().size());
for (MyMessageInbound messageInbound : EchoServlet.getSocketList()) {
CharBuffer msgBuffer1 = CharBuffer.wrap(msg1);
CharBuffer msgBuffer2 = CharBuffer.wrap(msg2);
WsOutbound outbound = messageInbound.getWsOutbound();
if (messageInbound.getUserIdName() != this.getUserIdName()) {
outbound.writeTextMessage(msgBuffer1);
outbound.flush();
} else {
outbound.writeTextMessage(msgBuffer2);
outbound.flush();
}
}
}
}