-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from yennanliu/chatroom-dev-002-add-channel
chatroom-dev-002-add-channel - add private, public channel
- Loading branch information
Showing
11 changed files
with
200 additions
and
65 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
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,21 +1,29 @@ | ||
## Ref | ||
|
||
- Article | ||
- https://blog.csdn.net/qqxx6661/article/details/98883166 | ||
- https://github.com/yennanliu/springboot-websocket-demo | ||
- https://blog.csdn.net/qqxx6661/article/details/98883166 | ||
- https://github.com/yennanliu/springboot-websocket-demo | ||
|
||
- https://ithelp.ithome.com.tw/articles/10197142 | ||
- https://ithelp.ithome.com.tw/articles/10197191 | ||
- https://www.baeldung.com/websockets-spring | ||
- https://blog.csdn.net/wwd0501/article/details/54582912 | ||
- https://www.slideshare.net/wenhsiaoyi/java-api-for-websocket | ||
- https://www.syscom.com.tw/ePaper_New_Content.aspx?id=368&EPID=194&TableName=sgEPArticle | ||
- https://tw511.com/a/01/16646.html | ||
- https://ithelp.ithome.com.tw/articles/10197142 | ||
- https://ithelp.ithome.com.tw/articles/10197191 | ||
- https://www.baeldung.com/websockets-spring | ||
- https://blog.csdn.net/wwd0501/article/details/54582912 | ||
- https://www.slideshare.net/wenhsiaoyi/java-api-for-websocket | ||
- https://www.syscom.com.tw/ePaper_New_Content.aspx?id=368&EPID=194&TableName=sgEPArticle | ||
- https://tw511.com/a/01/16646.html | ||
- Code | ||
- https://spring.io/guides/gs/messaging-stomp-websocket/ | ||
- https://morosedog.gitlab.io/springboot-20190416-springboot28/ | ||
- https://spring.io/guides/gs/messaging-stomp-websocket/ | ||
- https://morosedog.gitlab.io/springboot-20190416-springboot28/ | ||
|
||
- Video | ||
- https://youtu.be/r4fdPmZuzmY?si=qrmRTm09Bo53Ina1 | ||
- https://youtu.be/RbCFeeePJoM?si=DF6mES5XApvSkhXw | ||
- https://youtu.be/es_fTKyfI4w?si=vVhEz6Us-zrcerTR | ||
- https://youtu.be/r4fdPmZuzmY?si=qrmRTm09Bo53Ina1 | ||
- https://youtu.be/RbCFeeePJoM?si=DF6mES5XApvSkhXw | ||
- https://youtu.be/es_fTKyfI4w?si=vVhEz6Us-zrcerTR | ||
|
||
- Advanced | ||
- Chat with specific user | ||
- https://www.baeldung.com/spring-websockets-send-message-to-user | ||
- https://github.com/eugenp/tutorials/tree/master/spring-security-modules/spring-security-web-sockets - code | ||
- https://juejin.cn/post/6844903717636947981 | ||
|
||
- https://www.jb51.net/article/257091.htm |
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
26 changes: 26 additions & 0 deletions
26
springChatRoom/src/main/java/com/yen/springChatRoom/bean/OnlineUser.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,26 @@ | ||
package com.yen.springChatRoom.bean; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class OnlineUser { | ||
|
||
public OnlineUser(){ | ||
} | ||
|
||
public OnlineUser(List<String> users){ | ||
this.users = users; | ||
} | ||
|
||
private List<String> users; | ||
|
||
public List<String> getUsers() { | ||
return users; | ||
} | ||
|
||
public void setUsers(List<String> users) { | ||
this.users = users; | ||
} | ||
|
||
} |
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
53 changes: 53 additions & 0 deletions
53
springChatRoom/src/main/java/com/yen/springChatRoom/controller/UserController.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,53 @@ | ||
package com.yen.springChatRoom.controller; | ||
|
||
import com.yen.springChatRoom.bean.OnlineUser; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@RestController | ||
@RequestMapping("/user") | ||
public class UserController { | ||
|
||
@Value("${redis.channel.msgToAll}") | ||
private String msgToAll; | ||
|
||
@Value("${redis.set.onlineUsers}") | ||
private String onlineUsers; | ||
|
||
@Value("${redis.channel.userStatus}") | ||
private String userStatus; | ||
|
||
final String onlineUserKey = "websocket.onlineUsers"; | ||
|
||
@Autowired | ||
private RedisTemplate<String, String> redisTemplate; | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ChatController.class); | ||
|
||
@GetMapping("/online_user") | ||
public List<String> getOnlineUser(){ | ||
|
||
Set<String> resultSet = redisTemplate.opsForSet().members(onlineUserKey); | ||
System.out.println("(getOnlineUser) resultSet = " + resultSet); | ||
// TODO : optimize below | ||
OnlineUser onlineUser = new OnlineUser(); | ||
List<String> users = new ArrayList<>(); | ||
resultSet.forEach(x -> { | ||
users.add(x); | ||
}); | ||
onlineUser.setUsers(users); | ||
return onlineUser.getUsers(); | ||
} | ||
|
||
} |
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
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
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
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
32 changes: 32 additions & 0 deletions
32
springChatRoom/src/test/java/com/yen/springChatRoom/controller/ChatControllerTest.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,32 @@ | ||
package com.yen.springChatRoom.controller; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.data.redis.core.RedisOperations; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
class ChatControllerTest { | ||
|
||
@Autowired | ||
private RedisTemplate<String, String> redisTemplate; | ||
|
||
@Test | ||
public void testQueryRedis(){ | ||
|
||
final String onlineUserKey = "websocket.onlineUsers"; | ||
// https://ost.51cto.com/posts/2333 | ||
Set<String> resultSet = redisTemplate.opsForSet().members(onlineUserKey); | ||
System.out.println("resultSet:" + resultSet); | ||
|
||
} | ||
|
||
} |