java +jsp+mysql聊天室_Java WebSocket实现网络聊天室(群聊+私聊)

该博客介绍了如何使用Java WebSocket技术创建一个聊天室,包括群聊和私聊功能。内容涉及消息类的设计,如组装信息、消息内容、广播消息和对特定用户发送消息的方法,以及与在线用户列表的交互。
摘要由CSDN通过智能技术生成

package com.ccq.webSocket;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

import org.apache.commons.collections.CollectionUtils;

import org.apache.log4j.Logger;

import javax.websocket.Session;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;/**

* @author ccq

* @Description 消息类

* @date 2017/12/16 19:08*/

public classMessage {private static Logger logger = Logger.getLogger(Message.class);/**

* 消息类型*/

public static String NOTICE = "notice"; //通知

public static String MESSAGE = "message"; //消息

/**

* 组装信息返回给前台

* @param message 交互信息

* @param type 信息类型

* @param userList 在线列表

* @return

*

* "massage" : {

* "from" : "xxx",

* "to" : "xxx",

* "content" : "xxx",

* "time" : "xxxx.xx.xx"

* },

* "type" : {notice|message},

* "list" : {[xx],[xx],[xx]}*/

public static String getMessage(String message,String type,CollectionuserList){

JSONObject msg= newJSONObject();

msg.put("message",message);

msg.put("type", type);if(CollectionUtils.isNotEmpty(userList)){

List propertys = new ArrayList();

propertys.add("session");

JSONArray userListArray=JSONArray.fromObject(userList,JsonConfigUtils.getJsonConfig(propertys));

msg.put("list", userListArray);

}returnmsg.toString();

}/**

* 消息内容

* @param fromUser

* @param to

* @param content

* @param time

* @return

* {

* "from" : "xxx",

* "to" : "xxx",

* "content" : "xxx",

* "time" : "xxxx.xx.xx"

* }*/

public staticString getContent(OnlineUser fromUser,String to,String content,String time){

JSONObject contentJson= newJSONObject();//转化为json串时去掉session,用户session不能被序列化

List propertys = new ArrayList();

propertys.add("session");

contentJson.put("from",JSONObject.fromObject(fromUser,JsonConfigUtils.getJsonConfig(propertys)));

contentJson.put("to",to);

contentJson.put("content",content);

contentJson.put("time",time);returncontentJson.toString();

}/**

* 广播消息

* @param message 消息

* @param onlineUsers 在线用户*/

public static void broadcast(String message,CollectiononlineUsers){/***************************在线用户***************************/StringBuffer userStr= newStringBuffer();for(OnlineUser user : onlineUsers){

userStr.append(user.getNickname()+ ",");

}

userStr.deleteCharAt(userStr.length()-1);

logger.info("[broadcast] message =" + message + ", onlineUsers =" +userStr.toString());/***************************在线用户***************************/

for(OnlineUser user : onlineUsers){try{

user.getSession().getBasicRemote().sendText(message);

}catch(IOException e) {

e.printStackTrace();

logger.info("消息发送失败!" +e.getMessage());continue;

}

}

}/**

* 对特定用户发送消息

* @param message

* @param onlineUser*/

public static voidsingleSend(String message, OnlineUser onlineUser){

logger.info("[singleSend] message =" + message + ", toUser =" +onlineUser.getNickname());try{

onlineUser.getSession().getBasicRemote().sendText(message);

}catch(IOException e) {

e.printStackTrace();

logger.info("消息发送失败!" +e.getMessage());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值