需求:
老师跟学生聊天,要求实时的,而且老师跟学生要一对一的私聊。 不过我们的要求是学生跟学生之间无法聊天,这对实时聊天并不影响。
思路:
之前我从网上查了老半天,基本上都是多对多的群聊,要不就是建一个room,我实在是不知道怎么建room,忽然间看到了namespace的用法,我就觉得可以利用这个来做单对单聊天。
namespace的关键点:
比如你的浏览器监听的是http://localhost:10000/aaaa,那么你只能监听到 aaa 空间下的消息,如果你的浏览器监听的是http://localhost:10000/bbbb,那么你就只能监听到bbb空间下的消息.
解决方案:
在一个系统里,只要是user,就有唯一的userId,来识别每个人。那么我在每个用户登录成功后,获取他的id,用他的id来做每个人的namespace,这样保证了namespace的唯一性.
监听的事件,也只监听自己的namespace下的事件。
在发送消息时,肯定是知道对方的userId的。在向对方发送消息时,将对方的id作为namespace,发送一下消息,对方监听的是自己的namespace,那么就可以根据事件做一些后续操作.
解决步骤:
1.首先你要有jar包:socket.io.js
2.在登录成功后,去将id作为namespace,放到server中去:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIONamespace;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DataListener;
import com.zkhy.code2job.web.vo.common.TaskChatVo;
public class TaskChatLauncher {
private static Log logger = LogFactory.getLog(TaskChatLauncher.class);
private static Configuration config ;
public static SocketIOServer server ;
private long currentUserId;
public TaskChatLauncher(long currentUserId){
this.currentUserId=currentUserId;
}
public void startLisener(){
try {
//防止多次启动
if(server == null){
config = new Configuration();
config.setHostname("localhost");
config.setPort(10000);
server = new SocketIOServer(config);
server.start();
}
String namespace ="/"+ currentUserId;//构建命名空间
newNamespanceLisener(namespace);
} catch (Exception e) {
server.stop();
e.printStackTrace();
}
}
private void newNamespanceLisener(final String namespace) {
logger.info("监听实时对话 namespace:"+namespace);
final SocketIONamespace newChatnsp = server.addNamespace(namespace); //设置命名空间
//监听连接上的时候动作
newChatnsp.addConnectListener(new ConnectListener() {
@Override
public void onConnect(SocketIOClient arg0) {
logger.info("namespace:"+namespace+"连接上!");
}
});
//用namespace 来发送消息
newChatnsp.addEventListener("taskchatevent", TaskChatVo.class, new DataListener<TaskChatVo>() {
@Override
public void onData(SocketIOClient client, TaskChatVo data, AckRequest ackRequest) {
newChatnsp.getBroadcastOperations().sendEvent("taskchatevent", data);
}
});
}
}
注:currentUserId 就是登录成功获取id后传过来的,然后调用startLisener()方法。
3.这中间有个类TaskChatVo.java 是专门为聊天做的类,你可以根据具体需求去做:
public class TaskChatVo implements Serializable{
private static final long serialVersionUID = 1L;
/** 接收人 */
private long receiveUserId;
/** 发送人 */
private long sendUserId;
/** 内容 */
private String content;
public TaskChatVo(){}
public TaskChatVo(long id,long sendUserId,String content){
this.receiveUserId=id;
this.sendUserId=sendUserId;
this.content=content;
}
public long getReceiveUserId() {
return receiveUserId;
}
public void setReceiveUserId(long receiveUserId) {
this.receiveUserId = receiveUserId;
}
public long getSendUserId() {
return sendUserId;
}
public void setSendUserId(long sendUserId) {
this.sendUserId = sendUserId;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
4.前端js监听和发送消息:
var talk = null;
var senderId=$("#teacherId").val();
var receiverId=$("#selected-student-id").val();
var senderName=$("#teacherName").val();
var receiverName=$("#selected-student-name").val();
var taskId=$("#task-id").val();
//以上都是用来获取当前人的id,和你要发给谁的人的id.
var socket;
//断开socket连接
function sendDisconnect() {
socket.disconnect();
}
//消息中心 提醒
function appendChatContent(data) {
var content=data.content;
talk.append({text:content});
}
function setupIO() {
socket = io.connect('http://localhost:10000/'+senderId);
socket.on('connect', function() {
console.log('server connected.');
});
socket.on('taskchatevent', function(data) {
//监听当前跟谁聊天,避免,多个学生发向同一个窗口
if(data.sendUserId==receiverId){
appendChatContent(data);
}else{
//找学生列表的学生id,如果找到对应的,就一闪一闪。。。。
}
});
socket.on('disconnect', function() {
//TODO
});
}
//发送消息给对方
function emitContent(text){
// socket.disconnect();
socket=io.connect('http://localhost:10000/'+receiverId);
socket.emit("taskchatevent",{receiveUserId:receiverId,sendUserId:senderId,content:text});
/* socket.disconnect();不可以重新监听自己的信息 再使用下面的,相当于又建立一个连接。。。(所以隐了起来,放在这是提示自己不能这么做的原因)
setupIO();*/
}
$(function() {
setupIO();
/*下面这段的用到了一个插件,这个插件是自己写的,大概意思就是将内容 name神马的,显示到前端的一个框框中,自己可以做个简单的,或者不要直接写到一个div中也一样的。*/
talk = $('#tmessage').mdtalk({
me: {
_id: senderId,
name: senderName,
header: 'http://img.woyaogexing.com/2015/01/16/bccaafc6a51d0e47!200x200.png'
},
to: {
_id: receiverId,
name: receiverName,
header: 'http://img.woyaogexing.com/2015/01/15/93078204e588ef99!200x200.jpg'
},
//前端点击发送时会触发这个事件,因为我把聊天记录存储到了db中,所以还用到了ajax.
send: function(text) {
var date = new Date();
console.log(date.toLocaleDateString() + ' me: ' + text);
var promise = new Promise();
$.ajax({
url : addTaskTalkUrl,
data: {taskId:taskId,content:text,receiverId:receiverId},
type : 'POST',
dataType : 'json',
success:function(result){
//插入成功
if(result && result.taskTalkVo){
promise.resolve();
} else {
promise.reject();
}
//把内容 发送给对方监听
emitContent(text);
},
error: function() {
promise.reject();
}
});
return promise;
}
});
});
具体情况是这样的,老师可以跟多个学生聊天,当时老师T跟学生A聊天,学生A 和学生B同时向老师发送消息,当时老师打开的是A的窗口,结果B的信息也在A窗口上显示了。
就是老师只监听了自己的namespace,但是没有判断,当前他在跟谁聊天。(我们的需求是每个人当前只能跟一个人实时聊天)