springboot引入websocket
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter ( ) {
return new ServerEndpointExporter ( ) ;
}
}
import cn. hutool. core. bean. BeanUtil ;
import cn. hutool. core. collection. ConcurrentHashSet ;
import cn. hutool. extra. spring. SpringUtil ;
import com. alibaba. fastjson. JSONObject ;
import com. yunzan. app. domain. vo. LiveRoomTopThreeUserVo ;
import com. yunzan. app. domain. vo. LiveRoomTopThreeUserWrapperVo ;
import com. yunzan. app. service. impl. JyGiftRecordServiceImpl ;
import org. springframework. stereotype. Component ;
import org. springframework. stereotype. Service ;
import javax. websocket. * ;
import javax. websocket. server. PathParam ;
import javax. websocket. server. ServerEndpoint ;
import java. io. IOException ;
import java. math. BigDecimal ;
import java. util. List ;
import java. util. concurrent. ConcurrentHashMap ;
import java. util. concurrent. CopyOnWriteArraySet ;
@ServerEndpoint ( "/app/liveRoom/{roomId}/{userId}/{type}" )
@Component
public class WebSocketLiveRoomOnlineServer {
private static ConcurrentHashMap < String , ConcurrentHashSet < String > > userIdCollection = new ConcurrentHashMap < > ( ) ;
private static ConcurrentHashMap < String , CopyOnWriteArraySet < WebSocketLiveRoomOnlineServer > > liveRoomCollection = new ConcurrentHashMap < > ( ) ;
private String liveId;
private String userId;
private Session session;
private Integer type;
@OnOpen
public void onOpen ( Session session, @PathParam ( "roomId" ) String liveId, @PathParam ( "userId" ) String userId, @PathParam ( "type" ) Integer type) {
this . session = session;
this . userId = userId;
this . liveId = liveId;
this . type = type;
CopyOnWriteArraySet < WebSocketLiveRoomOnlineServer > roomMumberClient = liveRoomCollection. get ( liveId) ;
if ( BeanUtil . isNotEmpty ( roomMumberClient) ) {
roomMumberClient. add ( this ) ;
} else {
roomMumberClient = new CopyOnWriteArraySet < > ( ) ;
roomMumberClient. add ( this ) ;
liveRoomCollection. put ( liveId, roomMumberClient) ;
}
addOnlineCount ( liveId, userId, type) ;
System . out. println ( userId + "加入房间 " + liveId + "!当前人数为" + getOnlineCount ( liveId) ) ;
ConcurrentHashSet < String > userIds = userIdCollection. get ( liveId) ;
System . out. println ( "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" + userIds) ;
try {
publishMessage ( session, liveId, userIds) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
@OnClose
public void onClose ( ) throws IOException {
if ( this . session != null ) {
this . session. close ( ) ;
}
CopyOnWriteArraySet < WebSocketLiveRoomOnlineServer > roomMembers = liveRoomCollection. get ( this . liveId) ;
roomMembers. remove ( this ) ;
subOnlineCount ( this . liveId, this . userId) ;
ConcurrentHashSet < String > userIds = userIdCollection. get ( liveId) ;
publishMessage ( this . session, liveId, userIds) ;
System . out. println ( this . userId + "" + this . liveId + "断开webSocket连接!当前人数为" + getOnlineCount ( this . liveId) ) ;
}
public static synchronized int getOnlineCount ( String liveId) {
ConcurrentHashSet < String > uIds = userIdCollection. get ( liveId) ;
return uIds. size ( ) ;
}
public static synchronized void addOnlineCount ( String liveId, String userId, Integer type) {
if ( type == 2 ) {
ConcurrentHashSet < String > userIds = userIdCollection. get ( liveId) ;
if ( BeanUtil . isNotEmpty ( userIds) ) {
userIds. add ( userId) ;
} else {
ConcurrentHashSet < String > uId = new ConcurrentHashSet < > ( ) ;
uId. add ( userId) ;
userIdCollection. put ( liveId, uId) ;
}
}
}
public static synchronized void subOnlineCount ( String liveId, String userId) {
ConcurrentHashSet < String > userIds = userIdCollection. get ( liveId) ;
if ( BeanUtil . isNotEmpty ( userIds) ) {
userIds. remove ( userId) ;
}
}
public static ConcurrentHashSet < String > getUserIdCollection ( String liveId) {
return userIdCollection. get ( liveId) ;
}
public void publishMessage ( Session session, String liveId, ConcurrentHashSet < String > userIds) throws IOException {
int onlineCount = getOnlineCount ( liveId) ;
StringBuffer onlineCountStr = null ;
if ( onlineCount > 9999 ) {
onlineCountStr = new StringBuffer ( new BigDecimal ( onlineCount) . divide ( new BigDecimal ( 10000 ) , 1 , BigDecimal . ROUND_DOWN) . toString ( ) + "w" ) ;
} else {
onlineCountStr = new StringBuffer ( String . valueOf ( onlineCount) ) ;
}
LiveRoomTopThreeUserWrapperVo threeUserWrapper = new LiveRoomTopThreeUserWrapperVo ( ) ;
threeUserWrapper. setOnlineNum ( onlineCountStr. toString ( ) ) ;
JyGiftRecordServiceImpl jyGiftRecordService = SpringUtil . getBean ( JyGiftRecordServiceImpl . class ) ;
List < LiveRoomTopThreeUserVo > liveRoomTopThreeUserVos = jyGiftRecordService. queryRommTopThree ( liveId, userIds) ;
threeUserWrapper. setList ( jyGiftRecordService. queryRommTopThree ( liveId, userIds) ) ;
String res = JSONObject . toJSONString ( threeUserWrapper) ;
CopyOnWriteArraySet < WebSocketLiveRoomOnlineServer > roomMembers = liveRoomCollection. get ( this . liveId) ;
for ( WebSocketLiveRoomOnlineServer roomMember : roomMembers) {
roomMember. sendMessage ( res) ;
}
}
@OnMessage
public void onMessage ( String message) throws IOException {
}
public void sendMessage ( String message) throws IOException {
this . session. getBasicRemote ( ) . sendText ( message) ;
}
@OnError
public void onError ( Session session, Throwable throwable) {
System . out. println ( "发生错误" ) ;
throwable. printStackTrace ( ) ;
}
}
简单统计在线人数
@ServerEndpoint ( "/app/online/{userId}" )
@Component
public class WebSocketAppOnlineServer {
private static int onlineCount = 0 ;
private static CopyOnWriteArraySet < WebSocketAppOnlineServer > webSocketSet = new CopyOnWriteArraySet < WebSocketAppOnlineServer > ( ) ;
private Session session;
private Long userId = null ;
private static ConcurrentHashSet < Long > list = new ConcurrentHashSet ( ) ;
public static ConcurrentHashSet < Long > getList ( ) {
return list;
}
@OnOpen
public void onOpen ( Session session, @PathParam ( "userId" ) Long userId) {
this . session = session;
this . userId = userId;
if ( ! list. contains ( userId) ) {
list. add ( userId) ;
webSocketSet. add ( this ) ;
addOnlineCount ( ) ;
try {
sendMessage ( "连接成功!" ) ;
System . out. println ( "有新用户上线:" + userId + ",当前在线人数为:" + getOnlineCount ( ) ) ;
} catch ( IOException e) {
System . out. println ( "websocket IO Exception" ) ;
}
} else {
try {
list. add ( userId) ;
sendMessage ( "连接失败!该用户已连接了!" ) ;
System . out. println ( "有一个重复的窗口开始监听:" + userId + " 未将它添加至客户端" ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}