-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
112 changed files
with
16,580 additions
and
4,127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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应用 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
Tomcat-Websocket/.settings/org.eclipse.wst.common.project.facet.core.xml
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
Tomcat-Websocket/.settings/org.eclipse.wst.jsdt.ui.superType.container
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
Tomcat-Websocket/.settings/org.eclipse.wst.jsdt.ui.superType.name
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
63 changes: 63 additions & 0 deletions
63
websocket-chat/src/main/java/indi/anyesu/action/AudioController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
websocket-chat/src/main/java/indi/anyesu/action/BaseController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
websocket-chat/src/main/java/indi/anyesu/action/BaseMediaController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
websocket-chat/src/main/java/indi/anyesu/action/TextController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
Oops, something went wrong.