Spring框架也支持tcp连接,我编写下面的代码来设置一个简单的套接字服务器,我很困惑将下面的期货添加到我的套接字服务器:
>根据唯一标识符(例如从客户端收到的客户端密钥,可能使用TCP Connection Events)授权客户端
>直接向特定客户端发送消息(基于标识符)
>广播消息
更新:
>添加Config.sendMessage以向单个客户端发送消息
> Config.broadCast添加到广播消息
> authorizeIncomingConnection授权客户端,接受或拒绝连接
>添加tcpConnections静态字段以保留tcpEvent源
问题!
>正在使用tcpConnections HashMap好主意?!
>是我实施好的授权方法吗?
Main.java
@SpringBootApplication
public class Main {
public static void main(final String[] args) {
SpringApplication.run(Main.class,args);
}
}
Config.java
@EnableIntegration
@IntegrationComponentScan
@Configuration
public class Config implements ApplicationListenerfig.class.getName());
@Bean
public AbstractServerConnectionFactory AbstractServerConnectionFactory() {
return new TcpNetServerConnectionFactory(8181);
}
@Bean
public TcpInboundGateway TcpInboundGateway(AbstractServerConnectionFactory connectionFactory) {
TcpInboundGateway inGate = new TcpInboundGateway();
inGate.setConnectionFactory(connectionFactory);
inGate.setRequestChannel(getMessageChannel());
return inGate;
}
@Bean
public MessageChannel getMessageChannel() {
return new DirectChannel();
}
@MessageEndpoint
public class Echo {
@Transformer(inputChannel = "getMessageChannel")
public String convert(byte[] bytes) throws Exception {
return new String(bytes);
}
}
private static ConcurrentHashMapnopenEvent) {
LOGGER.info("Socket Opened " + source.getConnectionId());
tcpConnections.put(tcpEvent.getConnectionId(),source);
if (!authorizeIncomingConnection(source.getSocketInfo())) {
LOGGER.warn("Socket Rejected " + source.getConnectionId());
source.close();
}
} else if (tcpEvent instanceof TcpConnectionCloseEvent) {
LOGGER.info("Socket Closed " + source.getConnectionId());
tcpConnections.remove(source.getConnectionId());
}
}
private boolean authorizeIncomingConnection(SocketInfo socketInfo) {
//Authorization Logic,Like Ip,Mac Address WhiteList or anyThing else !
return (System.currentTimeMillis() / 1000) % 2 == 0;
}
public static String broadCast(String message) {
SetbroadCast Result,Success : " + successCounter + " Failure : " + FailureCounter;
}
public static void sendMessage(String connectionId,final String message) throws Exception {
tcpConnections.get(connectionId).send(new Message
MainController.java
@Controller
public class MainController {
@RequestMapping("/notify/{connectionId}/{message}")
@ResponseBody
public String home(@PathVariable String connectionId,@PathVariable String message) {
try {
Config.sendMessage(connectionId,message);
return "Client Notified !";
} catch (Exception e) {
return "Failed To Notify Client,cause : \n " + e.toString();
}
}
@RequestMapping("/broadCast/{message}")
@ResponseBody
public String home(@PathVariable String message) {
return Config.broadCast(message);
}
}
用法:
HTTP://本地主机:8080 /通知/ {的ConnectionId} / {消息}
> broadCast
HTTP://本地主机:8080 /广播/消息{}
>添加一个自定义路由器,跟踪每个连接的登录状态.
>查找连接ID,如果未经过身份验证,则路由到质询/响应子流.
>验证时,路由到正常流程.
要使用任意消息传递(而不是请求/响应),请使用TcpReceivingChannelAdapter和TcpsendingMessageHandler而不是入站网关.两者都配置为使用相同的连接工厂.对于发送到消息处理程序的每条消息,添加IpHeaders.CONNECTION_ID标头以定位特定客户端.
要广播,请为每个连接ID发送消息.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。