22 changed files with 1649 additions and 53 deletions
@ -1,4 +1,4 @@ |
|||||||
package net.sopod.soim.client.handler; |
package net.sopod.soim.client.cmd.handler; |
||||||
|
|
||||||
/** |
/** |
||||||
* CmdHandler |
* CmdHandler |
||||||
@ -0,0 +1,18 @@ |
|||||||
|
package net.sopod.soim.client.config; |
||||||
|
|
||||||
|
import com.google.inject.Singleton; |
||||||
|
import lombok.Data; |
||||||
|
|
||||||
|
/** |
||||||
|
* ClientConfig |
||||||
|
* |
||||||
|
* @author tmy |
||||||
|
* @date 2022-04-25 22:33 |
||||||
|
*/ |
||||||
|
@Singleton |
||||||
|
@Data |
||||||
|
public class ClientConfig { |
||||||
|
|
||||||
|
private String loginUrl = "http://localhost:3021/auth/pwdAuth"; |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,24 @@ |
|||||||
|
package net.sopod.soim.client.model.dto; |
||||||
|
|
||||||
|
import lombok.Data; |
||||||
|
|
||||||
|
/** |
||||||
|
* LoginResDTO |
||||||
|
* |
||||||
|
* @author tmy |
||||||
|
* @date 2022-04-25 22:36 |
||||||
|
*/ |
||||||
|
@Data |
||||||
|
public class LoginResDTO { |
||||||
|
|
||||||
|
private Boolean success; |
||||||
|
|
||||||
|
private Long uid; |
||||||
|
|
||||||
|
private String authToken; |
||||||
|
|
||||||
|
private Long expireMs; |
||||||
|
|
||||||
|
private String message; |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,81 @@ |
|||||||
|
package net.sopod.soim.client.session; |
||||||
|
|
||||||
|
import com.google.inject.Singleton; |
||||||
|
import com.google.protobuf.MessageLite; |
||||||
|
import io.netty.bootstrap.Bootstrap; |
||||||
|
import io.netty.channel.Channel; |
||||||
|
import io.netty.channel.ChannelInitializer; |
||||||
|
import io.netty.channel.nio.NioEventLoopGroup; |
||||||
|
import io.netty.channel.socket.SocketChannel; |
||||||
|
import io.netty.channel.socket.nio.NioSocketChannel; |
||||||
|
import net.sopod.soim.client.logger.Logger; |
||||||
|
import net.sopod.soim.core.net.ImMessageCodec; |
||||||
|
import net.sopod.soim.data.msg.auth.Auth; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean; |
||||||
|
|
||||||
|
/** |
||||||
|
* SoImSession |
||||||
|
* |
||||||
|
* @author tmy |
||||||
|
* @date 2022-04-25 22:49 |
||||||
|
*/ |
||||||
|
@Singleton |
||||||
|
public class SoImSession { |
||||||
|
|
||||||
|
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SoImSession.class); |
||||||
|
|
||||||
|
private NioEventLoopGroup eventLoopGroup; |
||||||
|
|
||||||
|
private Channel clientChannel; |
||||||
|
|
||||||
|
private AtomicBoolean auth = new AtomicBoolean(false); |
||||||
|
|
||||||
|
public void connect(String host, Integer port, Auth.ReqTokenAuth tokenAuth) throws InterruptedException { |
||||||
|
eventLoopGroup = new NioEventLoopGroup(2); |
||||||
|
Bootstrap b = new Bootstrap() |
||||||
|
.group(eventLoopGroup) |
||||||
|
.channel(NioSocketChannel.class) |
||||||
|
.handler(new ChannelInitializer<SocketChannel>() { |
||||||
|
@Override |
||||||
|
protected void initChannel(SocketChannel ch) throws Exception { |
||||||
|
ch.pipeline() |
||||||
|
.addLast(new ImMessageCodec()); |
||||||
|
// .addLast(new ProtoMessageCodec());
|
||||||
|
} |
||||||
|
}); |
||||||
|
clientChannel = b.connect(host, port).await().channel(); |
||||||
|
|
||||||
|
// 连接后立即发送认证消息
|
||||||
|
// TODO ResHandler
|
||||||
|
clientChannel.write(tokenAuth); |
||||||
|
} |
||||||
|
|
||||||
|
public void send(MessageLite message) { |
||||||
|
if (!auth.get()) { |
||||||
|
Logger.error("请登录后发送消息"); |
||||||
|
return; |
||||||
|
} |
||||||
|
if (clientChannel == null |
||||||
|
|| !clientChannel.isActive()) { |
||||||
|
Logger.error("连接未打开"); |
||||||
|
return; |
||||||
|
} |
||||||
|
clientChannel.write(message); |
||||||
|
} |
||||||
|
|
||||||
|
public void close() { |
||||||
|
if (clientChannel != null) { |
||||||
|
try { |
||||||
|
clientChannel.close().await(); |
||||||
|
} catch (InterruptedException e) { |
||||||
|
logger.error("channel close error: ", e); |
||||||
|
} |
||||||
|
} |
||||||
|
if (eventLoopGroup != null) { |
||||||
|
eventLoopGroup.shutdownGracefully(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,46 @@ |
|||||||
|
package net.sopod.soim.client.util; |
||||||
|
|
||||||
|
import com.google.common.net.MediaType; |
||||||
|
import kong.unirest.HttpResponse; |
||||||
|
import kong.unirest.JsonNode; |
||||||
|
import kong.unirest.Unirest; |
||||||
|
import kong.unirest.json.JSONElement; |
||||||
|
import net.sopod.soim.common.util.Jackson; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.net.URL; |
||||||
|
import java.net.URLConnection; |
||||||
|
import java.nio.charset.StandardCharsets; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* HttpClient |
||||||
|
* |
||||||
|
* @author tmy |
||||||
|
* @date 2022-04-25 22:06 |
||||||
|
*/ |
||||||
|
public class HttpClient { |
||||||
|
|
||||||
|
@SuppressWarnings("unchecked") |
||||||
|
public static <T> T restPost(String url, Map<?, ?> params, Class<T> resType) { |
||||||
|
HttpResponse<String> res = Unirest.post(url) |
||||||
|
.body(Jackson.json().serialize(params)) |
||||||
|
.contentType("application/json") |
||||||
|
.charset(StandardCharsets.UTF_8) |
||||||
|
.asString(); |
||||||
|
if (String.class.equals(resType)) { |
||||||
|
return (T) res.getBody(); |
||||||
|
} |
||||||
|
return Jackson.json().deserialize(res.getBody(), resType); |
||||||
|
} |
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException { |
||||||
|
HashMap<String, String> params = new HashMap<>(); |
||||||
|
params.put("account", "prometheus"); |
||||||
|
params.put("password", "123456"); |
||||||
|
String res = restPost("http://localhost:3021/auth/pwdAuth", params, String.class); |
||||||
|
System.out.println(res); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,8 @@ |
|||||||
net.sopod.soim.data.msg.net.HeartBeat$Ping=10000 |
net.sopod.soim.data.msg.auth.Auth$ReqTokenAuth=10000 |
||||||
net.sopod.soim.data.msg.net.HeartBeat$Pong=10001 |
net.sopod.soim.data.msg.hello.HelloPB$World=10001 |
||||||
net.sopod.soim.data.msg.auth.Auth$ResLogin=10002 |
net.sopod.soim.data.msg.net.HeartBeat$Ping=10002 |
||||||
net.sopod.soim.data.msg.hello.HelloPB$Hello=10003 |
net.sopod.soim.data.msg.net.HeartBeat$Pong=10003 |
||||||
net.sopod.soim.data.msg.auth.Auth$ReqLogin=10004 |
net.sopod.soim.data.msg.hello.HelloPB$Hello=10004 |
||||||
net.sopod.soim.data.msg.hello.HelloPB$World=10005 |
net.sopod.soim.data.msg.auth.Auth$ReqLogin=10005 |
||||||
|
net.sopod.soim.data.msg.auth.Auth$ResTokenAuth=10006 |
||||||
|
net.sopod.soim.data.msg.auth.Auth$ResLogin=10007 |
||||||
|
|||||||
Loading…
Reference in new issue