Skip to content

Commit

Permalink
重构
Browse files Browse the repository at this point in the history
  • Loading branch information
anyesu committed Dec 31, 2017
1 parent 65e58ce commit d436477
Show file tree
Hide file tree
Showing 112 changed files with 16,580 additions and 4,127 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
# 这是基于websocket的在线聊天室demo
###主要功能包括文本、视频、语音的通讯
其中,Tomcat-Websocket项目是基于tomcat服务器的java版服务端实现,Nodejs-Websocket项目是基于Nodejs的js版服务端实现。两个项目的服务端逻辑基本一致,客户端代码可以通用。此外,前者所有功能都基于websocket实现,后者采用webrtc技术实现语音视频的通讯,相比之下技术更成熟,性能更好。
# websocket应用
12 changes: 0 additions & 12 deletions Tomcat-Websocket/.settings/.jsdtscope

This file was deleted.

7 changes: 0 additions & 7 deletions Tomcat-Websocket/.settings/org.eclipse.jdt.core.prefs

This file was deleted.

8 changes: 0 additions & 8 deletions Tomcat-Websocket/.settings/org.eclipse.wst.common.component

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<name>websocket</name>
<artifactId>websocket</artifactId>
<packaging>pom</packaging>

<parent>
<groupId>indi.anyesu.websocket</groupId>
<artifactId>websocket-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>websocket-parent/pom.xml</relativePath>
</parent>

<modules>
<module>websocket-core</module>
<module>websocket-chat</module>
</modules>
</project>
45 changes: 45 additions & 0 deletions websocket-chat/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>websocket-chat</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>websocket-chat</name>

<parent>
<groupId>indi.anyesu.websocket</groupId>
<artifactId>websocket-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../websocket-parent/pom.xml</relativePath>
</parent>

<properties>
</properties>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>indi.anyesu.websocket</groupId>
<artifactId>websocket-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

</dependencies>

<build>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package indi.anyesu.action;

import javax.websocket.EndpointConfig;
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 java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
* Websocket 音频通讯
*
* @author anyesu
*/
@ServerEndpoint(value = "/websocket/chat/audio", configurator = WsConfigurator.class)
public class AudioController extends BaseMediaController {

private static final List<AbstractWsController> CONNECTIONS = new CopyOnWriteArrayList<>();

@Override
@OnOpen
public void onOpen(Session session, EndpointConfig config) {
super.onOpen(session, config);
}

@Override
@OnClose
public void onClose() {
super.onClose();
}

@Override
@OnMessage(maxMessageSize = 10000000)
public void onMessage(String message) {
super.onMessage(message);
}

@Override
@OnMessage(maxMessageSize = 10000000)
public void onMessage(ByteBuffer message) {
super.onMessage(message);
}

@Override
@OnError
public void onError(Throwable t) {
}

@Override
List<AbstractWsController> getConnections() {
return CONNECTIONS;
}

@Override
String getConnectType() {
return "audio";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package indi.anyesu.action;

import com.alibaba.fastjson.JSONObject;
import indi.anyesu.model.Message;
import indi.anyesu.util.StringUtil;

/**
* 公共逻辑
*
* @author anyesu
*/
public abstract class BaseController extends AbstractWsController {

private static final String CONNECT_TYPE_TEXT = "text";

/**
* 接受客户端发送的字符串
*
* @param message 字符串消息
*/
@Override
protected void onMessage(String message) {
Message msg = JSONObject.parseObject(message, Message.class);
msg.setHost(getUserName());
if (CONNECT_TYPE_TEXT.equals(getConnectType())) {
msg.setMsg(StringUtil.txt2htm(msg.getMsg()));
if (msg.getDests() == null) {
broadcast2All(msg.toString());
} else {
broadcast2Special(msg.toString(), msg.getDests());
}
} else {
broadcast2Others(msg.toString());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package indi.anyesu.action;

import javax.websocket.EndpointConfig;
import javax.websocket.Session;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* 公共逻辑
*
* @author anyesu
*/
public abstract class BaseMediaController extends BaseController {

@Override
public void onOpen(Session session, EndpointConfig config) {
// 设置用户信息
Map<String, List<String>> map = session.getRequestParameterMap();
setSession(session);
List<String> uids = map.get("uid");
if (uids == null) {
try {
this.getSession().close();
} catch (IOException ignored) {
}
} else {
setUserName(uids.get(0));
super.onOpen(session, config);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package indi.anyesu.action;

import indi.anyesu.model.IdGenerator;
import indi.anyesu.model.Message;
import indi.anyesu.model.Message.MsgConstant;
import indi.anyesu.model.Message.RoomInfo;

import javax.websocket.EndpointConfig;
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 java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
* Websocket 文字通讯
*
* @author anyesu
*/
@ServerEndpoint(value = "/websocket/chat", configurator = WsConfigurator.class)
public class TextController extends BaseController {

private static final List<AbstractWsController> CONNECTIONS = new CopyOnWriteArrayList<>();

private RoomInfo roomInfo;

@Override
@OnOpen
public void onOpen(Session session, EndpointConfig config) {
// 设置用户信息
setUserName(IdGenerator.getNextId());
setSession(session);
// 设置聊天室信息
if (CONNECTIONS.size() == 0) {
setRoomInfo(new RoomInfo(getUserName(), (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date())));
} else {
Iterator<AbstractWsController> it = CONNECTIONS.iterator();
TextController client = (TextController) it.next();
setRoomInfo(client.getRoomInfo());
}
Message msg = new Message(getUserName(), MsgConstant.SET_NAME);
msg.setRoomInfo(getRoomInfo());
call(msg.toString());
super.onOpen(session, config);
}

@Override
@OnClose
public void onClose() {
super.onClose();
}

@Override
@OnMessage(maxMessageSize = 10000000)
public void onMessage(String message) {
super.onMessage(message);
}

@Override
@OnMessage(maxMessageSize = 10000000)
public void onMessage(ByteBuffer message) {
super.onMessage(message);
}

@Override
@OnError
public void onError(Throwable t) {
}

@Override
List<AbstractWsController> getConnections() {
return CONNECTIONS;
}

/**
* 设置聊天室信息
*/
private void setRoomInfo(RoomInfo roomInfo) {
this.roomInfo = roomInfo;
}

private RoomInfo getRoomInfo() {
return roomInfo;
}

@Override
String getConnectType() {
return "text";
}

}
Loading

0 comments on commit d436477

Please sign in to comment.