下面列出了javax.servlet.ServletContextAttributeEvent#org.apache.catalina.websocket.WsOutbound 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void onOpen(WsOutbound outbound) {
if(uid == -1){ //过期用户直接下线
Map<String, Object> outMap = new HashMap<String, Object>();
outMap.put("response", "notlogin");
String jsonString = JSONObject.fromObject(outMap).toString();
CharBuffer buffer = CharBuffer.wrap(jsonString);
try {
outbound.writeTextMessage(buffer);
outbound.flush();
} catch (IOException e) {
e.printStackTrace();
}
}else{
userMap.put(uid, outbound);
System.out.println("[上线]==>uid:"+uid+"在线用户==>"+userMap.size());
sendUnread(outbound); //登录即检查有没有未读消息
}
super.onOpen(outbound);
}
private void sendUnread(WsOutbound outbound){
Map<String, Object> outMap = new HashMap<String, Object>();
outMap.put("response", "remind_unread");
UnreadDAO dao = new UnreadDAOimpl();
List<UnreadVo> list = dao.getUnread(uid);
if(list != null){
outMap.put("remind_unread", list);
String jsonString = JSONObject.fromObject(outMap).toString();
CharBuffer buffer = CharBuffer.wrap(jsonString);
try {
outbound.writeTextMessage(buffer);
outbound.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@SuppressWarnings("unchecked")
@Override
public void attributeAdded(ServletContextAttributeEvent arg0) {
if(userMap == null){
userMap = (Map<Integer, WsOutbound>) arg0.getServletContext().getAttribute("OnLineList");
}
//System.out.println("listener==>attributeAdded");
Enumeration<String> att = arg0.getServletContext().getAttributeNames();
while(att.hasMoreElements()){
String next = att.nextElement();
if(next.startsWith("action")){
ServerMsg message = (ServerMsg) arg0.getServletContext().getAttribute(next);
if(message != null){
arg0.getServletContext().removeAttribute(next);
doAction(message);
}
}
}
}
private void sendUnread(UnreadVo unread, WsOutbound outbound) {
Map<String, Object> outMap = new HashMap<String, Object>();
outMap.put("response", "remind_unread");
List<UnreadVo> list = new ArrayList<UnreadVo>();
list.add(unread);
outMap.put("remind_unread", list);
String jsonString = JSONObject.fromObject(outMap).toString();
CharBuffer buffer = CharBuffer.wrap(jsonString);
try {
outbound.writeTextMessage(buffer);
outbound.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private void action10(ServerMsg message){
EventFollowerVo action = (EventFollowerVo) message.getAction();
int eid = action.getEid();
String uname = action.getUname();
dao.countFollower(eid); //活动参与人数计数+1
//发送给活动创建者
int euid = dao.getEventUID(eid);
WsOutbound outbound = userMap.get(euid);
if(outbound != null){
UnreadVo unread = dao.putUnread(euid,3,action.getEid(),0,uname+"加入了你的活动",DateTimeUtil.currentTime(),true);
unread.setIsread(false);
sendUnread(unread,outbound);
}else{
dao.putUnread(euid,3,action.getEid(),0,uname+"加入了你的活动",DateTimeUtil.currentTime(),false);
}
}
public void action15(ServerMsg message){
EventVo action = (EventVo) message.getAction();
int eid = action.getEid();
List<Integer> uids = dao.getAllFollowerUser(eid); //通知所有参与的用户
for(int uid:uids){
WsOutbound outbound = userMap.get(uid);
if(outbound != null){
UnreadVo unread = dao.putUnread(uid,4,eid,1,action.getOutline(),DateTimeUtil.currentTime(),true);
unread.setIsread(false);
sendUnread(unread,outbound);
}else{
dao.putUnread(uid,4,eid,1,action.getOutline(),DateTimeUtil.currentTime(),false);
}
}
dao.deleteFollower(eid); //删除所有参与者,补充和评论
dao.deleteAdd(eid);
dao.deleteComment(eid);
}
@Override
protected void onBinaryData(InputStream is) throws IOException {
// Simply echo the data to back to the client.
WsOutbound outbound = getWsOutbound();
int i = is.read();
while (i != -1) {
outbound.writeBinaryData(i);
i = is.read();
}
outbound.flush();
}
@Override
protected void onTextData(Reader r) throws IOException {
// Simply echo the data to back to the client.
WsOutbound outbound = getWsOutbound();
int c = r.read();
while (c != -1) {
outbound.writeTextData((char) c);
c = r.read();
}
outbound.flush();
}
@Override
protected void onOpen(WsOutbound outbound) {
connections.add(this);
String message = String.format("* %s %s",
nickname, "has joined.");
broadcast(message);
}
@Override
protected void onBinaryData(InputStream is) throws IOException {
// Simply echo the data to back to the client.
WsOutbound outbound = getWsOutbound();
int i = is.read();
while (i != -1) {
outbound.writeBinaryData(i);
i = is.read();
}
outbound.flush();
}
@Override
protected void onTextData(Reader r) throws IOException {
// Simply echo the data to back to the client.
WsOutbound outbound = getWsOutbound();
int c = r.read();
while (c != -1) {
outbound.writeTextData((char) c);
c = r.read();
}
outbound.flush();
}
@Override
protected void onOpen(WsOutbound outbound) {
connections.add(this);
String message = String.format("* %s %s",
nickname, "has joined.");
broadcast(message);
}
@Override
protected void onBinaryData(InputStream is) throws IOException {
// Simply echo the data to back to the client.
WsOutbound outbound = getWsOutbound();
int i = is.read();
while (i != -1) {
outbound.writeBinaryData(i);
i = is.read();
}
outbound.flush();
}
@Override
protected void onTextData(Reader r) throws IOException {
// Simply echo the data to back to the client.
WsOutbound outbound = getWsOutbound();
int c = r.read();
while (c != -1) {
outbound.writeTextData((char) c);
c = r.read();
}
outbound.flush();
}
@Override
protected void onOpen(WsOutbound outbound) {
connections.add(this);
String message = String.format("* %s %s",
nickname, "has joined.");
broadcast(message);
}
public void action14(ServerMsg message){
EventAddVo action = (EventAddVo) message.getAction();
int eid = action.getEid();
List<Integer> uids = dao.getAllFollowerUser(eid);
for(int uid:uids){
WsOutbound outbound = userMap.get(uid);
if(outbound != null){
UnreadVo unread = dao.putUnread(uid,0,eid,0,action.getContent(),DateTimeUtil.currentTime(),true);
unread.setIsread(false);
sendUnread(unread,outbound);
}else{
dao.putUnread(uid,0,eid,0,action.getContent(),DateTimeUtil.currentTime(),false);
}
}
}
/**
* Sends send the result of {@link Serializer#serialize(List)} to a destination.
*
* @param buffer the bytes to send.
* @param destination The peer to send to.
*/
private void send(final byte[] buffer, final Object destination) {
if (LOG.isTraceEnabled()) {
LOG.trace("Sending from thread: id: " + Thread.currentThread().getName() + ", name: "
+ Thread.currentThread().getName());
}
final WsOutbound outbound = ((MessageInbound) destination).getWsOutbound();
final ExecutorService executorService = connectionThreads.get(destination);
// execute asynchronously to avoid slower clients from interfering with faster clients
executorService.execute(new Runnable() {
@Override
public void run() {
try {
outbound.writeBinaryMessage(ByteBuffer.wrap(buffer));
} catch (final IOException e) {
LOG.warn("Sending data to a client failed. Closing connection to this client.");
try {
outbound.close(1002, null);
// CHECKSTYLE:OFF
} catch (final IOException e1) {
// Maybe the connection is already closed. This is no exceptional state but rather the
// default in
// this case. So it's safe to ignore this exception.
}
// CHECKSTYLE:ON
connectionCloses((SynchronizeFXTomcatConnection) destination);
}
}
});
}
public Snake(int id, WsOutbound outbound) {
this.id = id;
this.outbound = outbound;
this.hexColor = SnakeWebSocketServlet.getRandomHexColor();
resetState();
}
public Snake(int id, WsOutbound outbound) {
this.id = id;
this.outbound = outbound;
this.hexColor = SnakeWebSocketServlet.getRandomHexColor();
resetState();
}
public Snake(int id, WsOutbound outbound) {
this.id = id;
this.outbound = outbound;
this.hexColor = SnakeWebSocketServlet.getRandomHexColor();
resetState();
}
@Override
protected void onOpen(final WsOutbound outbound) {
parent.clientConnectionReady(this);
}
/**
* Sends the given Guacamole and WebSocket numeric status
* on the given WebSocket connection and closes the
* connection.
*
* @param outbound
* The outbound WebSocket connection to close.
*
* @param guacamoleStatusCode
* The status to send.
*
* @param webSocketCode
* The numeric WebSocket status code to send.
*/
private void closeConnection(WsOutbound outbound, int guacamoleStatusCode,
int webSocketCode) {
try {
byte[] message = Integer.toString(guacamoleStatusCode).getBytes("UTF-8");
outbound.close(webSocketCode, ByteBuffer.wrap(message));
}
catch (IOException e) {
logger.debug("Unable to close WebSocket tunnel.", e);
}
}
/**
* Sends the given status on the given WebSocket connection
* and closes the connection.
*
* @param outbound
* The outbound WebSocket connection to close.
*
* @param guacStatus
* The status to send.
*/
private void closeConnection(WsOutbound outbound,
GuacamoleStatus guacStatus) {
closeConnection(outbound, guacStatus.getGuacamoleStatusCode(),
guacStatus.getWebSocketCode());
}
/**
* Sends the given Guacamole and WebSocket numeric status
* on the given WebSocket connection and closes the
* connection.
*
* @param outbound
* The outbound WebSocket connection to close.
*
* @param guacamoleStatusCode
* The status to send.
*
* @param webSocketCode
* The numeric WebSocket status code to send.
*/
private void closeConnection(WsOutbound outbound, int guacamoleStatusCode,
int webSocketCode) {
try {
byte[] message = Integer.toString(guacamoleStatusCode).getBytes("UTF-8");
outbound.close(webSocketCode, ByteBuffer.wrap(message));
}
catch (IOException e) {
logger.debug("Unable to close WebSocket tunnel.", e);
}
}
/**
* Sends the given status on the given WebSocket connection
* and closes the connection.
*
* @param outbound
* The outbound WebSocket connection to close.
*
* @param guacStatus
* The status to send.
*/
private void closeConnection(WsOutbound outbound,
GuacamoleStatus guacStatus) {
closeConnection(outbound, guacStatus.getGuacamoleStatusCode(),
guacStatus.getWebSocketCode());
}