package com.media.controller;
import java.io.IOException;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import com.alibaba.fastjson.JSON;
import com.media.code.MessageStructCode;
import com.media.code.MessageTypeCode;
import com.media.code.OneReadyResponseCode;
import com.media.service.IndexService;
import com.media.storage.PersonalSessionStorage;
@ServerEndpoint(value="/index")
public class IndexController {
@OnOpen
public void open(Session session){
String id = session.getId();
try{
PersonalSessionStorage.addSessionById(id, session);
String ret = getRet(MessageTypeCode.PERSONAL_ID, id);
session.getBasicRemote().sendText(ret);
}catch(Throwable e){
PersonalSessionStorage.delSessionById(id);
try {
session.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
@OnMessage
public void OnMessage(String message, Session session){
try{
MessageStructCode struct = JSON.parseObject(message,MessageStructCode.class);
switch (struct.getKey()) {
case QUERY_ID://搜索个人
session.getBasicRemote().sendText(getRet(MessageTypeCode.QUERY_ID,IndexService.queryId(session.getId(),struct.getValue().toString())));
break;
case READY_FOR_ONE://一对一准备
Session answerSession = IndexService.readyForOne(session.getId(), struct.getValue().toString());
if(answerSession != null){
answerSession.getBasicRemote().sendText(getRet(MessageTypeCode.READY_FOR_ONE,session.getId()));//给对方发送准备请求
}
break;
case READY_FOR_ONE_RESPONSE://一对一准备响应
OneReadyResponseCode ret = IndexService.readyForOneResponse(session.getId(),Boolean.parseBoolean(struct.getValue().toString()));
Session readySession = ret.getSession();
ret.setSession(null);
readySession.getBasicRemote().sendText(getRet(MessageTypeCode.READY_FOR_ONE_RESPONSE,JSON.toJSONString(ret)));
break;
case SIGNALLING_OFFER://接收发送到服务端的信令
OneReadyResponseCode offerRet = IndexService.signallingOffer(session.getId());
if(offerRet.isStatus()){//转发信令
offerRet.getSession().getBasicRemote().sendText(getRet(MessageTypeCode.SIGNALLING_ANSWER,JSON.toJSONString(struct.getValue())));
}else{//非法发送,响应回去
Session tempSession = offerRet.getSession();
offerRet.setSession(null);
tempSession.getBasicRemote().sendText(getRet(MessageTypeCode.READY_FOR_ONE_RESPONSE,JSON.toJSONString(offerRet)));
}
break;
case ONE_CHANNEL_CLOSE://发送了一对一通道关闭
Session closeSession = IndexService.oneChannelClose(session.getId());
if(closeSession != null){
closeSession.getBasicRemote().sendText(getRet(MessageTypeCode.ONE_CHANNEL_CLOSE,true));
}
break;
default:
break;
}
}catch(Throwable e){
e.printStackTrace();
}
}
@OnError
public void onError(Throwable t) {
System.out.println(t.getMessage());
}
@OnClose
public void close(Session session){//当关闭浏览器后,则删除session记录和对话准备
try{
//并且给对方 发送关闭对话的通知,如果有在对话
Session closeSession = IndexService.oneChannelClose(session.getId());
if(closeSession != null){
closeSession.getBasicRemote().sendText(getRet(MessageTypeCode.ONE_CHANNEL_CLOSE,true));
}
PersonalSessionStorage.delSessionById(session.getId());
}catch(Throwable e){
e.printStackTrace();
}
}
/**
* 发送消息结构
* @param key
* @param msg
* @return
*/
private String getRet(MessageTypeCode key,Object msg){
MessageStructCode struct = new MessageStructCode();
struct.setValue(msg);
struct.setKey(key);
return JSON.toJSONString(struct);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java + webrtc 视频聊天室,一对一,多对多,文件传输
共17个文件
java:8个
js:3个
jsp:2个
4星 · 超过85%的资源 需积分: 2 160 下载量 70 浏览量
2018-02-09
18:08:47
上传
评论 11
收藏 36KB ZIP 举报
温馨提示
java + webrtc 实现的 视频聊天室 , 多对多的这个资源没有实现,这个资源只实现了一对一,后续的代码会在 github上持续更新 地址为: https://github.com/js1688/cat 成果展示地址:https://www.tanjun.xyz webrtc 真正的使用起来要求的东西比较多,如果只在本机上跑没有意义.
资源推荐
资源详情
资源评论
收起资源包目录
cat.zip (17个子文件)
cat
src
test
java
main
webapp
WEB-INF
web.xml 222B
index_bak.jsp 24KB
index.jsp 6KB
img
ok.png 14KB
js
index3.js 3KB
index2.js 365B
index1.js 5KB
css
index.css 1KB
resources
java
com
media
service
IndexService.java 3KB
code
OneReadyResponseCode.java 796B
MessageTypeCode.java 796B
MessageStructCode.java 429B
OneReadyCode.java 489B
controller
IndexController.java 4KB
storage
PersonalSessionStorage.java 952B
PersonalReadyStorage.java 3KB
pom.xml 1KB
共 17 条
- 1
资源评论
- yj3021482019-09-12下载了,可以用,还不错
- qq_422068482018-09-13功能很强。
- keyeremail2020-04-16为什么 我用不了各种JS错误
- emanresu2020-04-10jsp项目,太老了
jflove.cn
- 粉丝: 9
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功