jssmp是Java版SSMP开源实现。
依赖:
JDK1.8orhigher
netty3.10+
slf4j1.7+
guava17+
客户端
// given: SSLEngine sslEngine;SSMPClient c = new SSMPClient("localhost", 1234, new HashedWheelTimer(), new NioClientSocketChannelFactory(), () -> new SslHandler(sslEngine), e -> System.out.println("event: " + e));c.connect(SSMPIdentifier.fromInternal("foo"), SSMPIdentifier.fromInternal("secret"), "NotARealSecret", new ConnectionListener() { @Override public void connected() { System.out.println("connected"); } @Override public void disconnected() { System.out.println("disconnected"); } });SSMPResponse r = c.request(SSMPRequest.ucast(SSMPIdentifier.fromInternal("bar"), "Hello World!")).get();System.out.println("response: " + r);服务器端
// given: SSLEngine sslEngine;SSMPServer s = new SSMPServer(new InetSocketAddress("localhost", 1234), new HashedWheelTimer(), new NioServerSocketChannelFactory(), () -> new SslHandler(sslEngine), new Authenticator() { @Override public boolean authenticate(SSMPIdentifier id, SSMPIdentifier scheme, String cred) { return scheme.toString().equals("secret") && cred.equals("NotARealSecret"); } @Override public ChannelBuffer unauthorized() { return ChannelBuffers.wrappedBuffer("401 secret\n".getBytes(StandardCharsets.US_ASCII)); } });s.start();
评论