-
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.
add online user, controller, js, remove legacy code, add junit dep
- Loading branch information
Showing
11 changed files
with
180 additions
and
52 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
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); | ||
|
||
} | ||
|
||
} |