-
Notifications
You must be signed in to change notification settings - Fork 25
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
7 changed files
with
427 additions
and
186 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
88 changes: 88 additions & 0 deletions
88
src/main/java/org/b3log/symphony/processor/channel/LoginChannel.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,88 @@ | ||
package org.b3log.symphony.processor.channel; | ||
|
||
import org.b3log.latke.http.WebSocketChannel; | ||
import org.b3log.latke.http.WebSocketSession; | ||
import org.b3log.latke.ioc.Singleton; | ||
import org.json.JSONObject; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.Map; | ||
/** | ||
* Login channel. | ||
* | ||
* @author <a href="https://fishpi.cn/member/Yui">Yui</a> | ||
* @version 1.0.0.0, Dec 4, 2024 | ||
*/ | ||
@Singleton | ||
public class LoginChannel implements WebSocketChannel { | ||
/** | ||
* 已经连接的WebSocket客户端列表 | ||
*/ | ||
private final Map<String, WebSocketSession> sessions = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public void onConnect(WebSocketSession webSocketSession) { | ||
// 把客户端存入Map,方便发信息 | ||
sessions.put(webSocketSession.getId(), webSocketSession); | ||
} | ||
|
||
@Override | ||
public void onMessage(Message message) { | ||
try { | ||
JSONObject jsonObject = new JSONObject(message.text); | ||
String targetId; | ||
String apiKey; | ||
WebSocketSession targetSession; | ||
switch (jsonObject.get("type").toString()) { | ||
case "1": | ||
// type1: 获取web端websocket id 方便 APP 单独向指定的web发送apikey | ||
message.session.sendText("targetId:" + message.session.getId()); | ||
break; | ||
case "2": | ||
// type2: APP 携带apiKey发送登录 | ||
targetId = jsonObject.get("targetId").toString(); | ||
apiKey = jsonObject.get("apiKey").toString(); | ||
if (targetId == null) { | ||
message.session.sendText("{\"code\":1,\"msg\":\"Params is not right\"}"); | ||
break; | ||
} | ||
targetSession = sessions.get(targetId); | ||
if (targetSession != null) { | ||
targetSession.sendText("apiKey:" + apiKey); | ||
message.session.sendText("{\"code\":0,\"msg\":\"Success\"}"); | ||
} else { | ||
message.session.sendText("{\"code\":1,\"msg\":\"Target is not exist\"}"); | ||
} | ||
break; | ||
case "3": | ||
// type3: APP 已经扫码,但是还没调用登录 (后续APP如果加点击确认按钮了,这个是中间状态) | ||
targetId = jsonObject.get("targetId").toString(); | ||
if (targetId == null) { | ||
message.session.sendText("{\"code\":1,\"msg\":\"Params is not right\"}"); | ||
break; | ||
} | ||
targetSession = sessions.get(targetId); | ||
if (targetSession != null) { | ||
targetSession.sendText("scan:ing~"); | ||
message.session.sendText("{\"code\":0,\"msg\":\"Success\"}"); | ||
} else { | ||
message.session.sendText("{\"code\":1,\"msg\":\"Target is not exist\"}"); | ||
} | ||
default: | ||
break; | ||
} | ||
} catch (Exception e) { | ||
message.session.sendText("{\"code\":1,\"msg\":\"Message parse error\"}"); | ||
} | ||
} | ||
|
||
@Override | ||
public void onClose(WebSocketSession webSocketSession) { | ||
// 将关闭连接的websocket从Map中移除 | ||
sessions.remove(webSocketSession.getId()); | ||
} | ||
|
||
@Override | ||
public void onError(Error error) { | ||
} | ||
} |
Large diffs are not rendered by default.
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
Oops, something went wrong.