Java server demo environment
Introduce the maven coordinates of the netty framework encapsulated by the third package in the pom file
<dependency>
<groupId>io.github.fzdwx</groupId>
<artifactId>sky-http-springboot-starter</artifactId>
<version>0.10.6</version>
</dependency>
* Note : The springbootweb starter needs to be commented out in the pom file. The web starter defaults to starting the tomcat service, which will conflict with the netty service
Create a java server and create it in interface mode, which is convenient for external calls
@GetMapping("/getConnect")
public void getConnect(HttpServerRequest request){
request.upgradeToWebSocket(ws -> {
ws.mountOpen(h->{
ws.send("连接成功,开始聊天吧!");
});
ws.mountText(s -> {
System.out.println(s);
//对方回复
System.out.println("客户端回复: "+s);
//获取控制台输入的值
Scanner scanner =new Scanner(System.in);
String next = scanner.next();
ws.send(next);
});
});
}
Start the service, the following information appears to indicate that the startup is successful, and the exposed port default: 9999
Then expose the local service to the public network through cpolar. For more information about the installation and use of cpolar, please refer to the following articles:
Remote company intranet server [intranet penetration ]
Open the browser to access: http://127.0.0.1:9200 cpolar web ui interface, create a tunnel with tcp port 9999
Note : The tunnel selects a temporary tcp address and port, which will change within 24 hours. If you need to fix the tcp address, you can upgrade to a professional package to fix the tcp address!
View Status -> Online Tunnel, copy the public network address and port of the created tunnel
At this point, the websocket server has been exposed from the local localhost to the public network, and then we create a client to test the public network to access the socket server connection
Take the go-based socket client as an example, connect the java socket server through the public network
Download the websocket framework via git
go get github.com/gorilla/websocket
Create a GO client, note: the Host value is the public network address of the tunnel copied above!!
package main
import (
"fmt"
"log"
"net/url"
"github.com/gorilla/websocket"
)
func main() {
// 定义服务端的地址
u := url.URL{
Scheme: "ws",
Host: "3.tcp.vip.cpolar.cn:10793", //地址为复制隧道的公网地址
Path: "/eth/getConnect"} //服务端controller 映射地址
// 与服务端建立连接
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial:", err)
}
defer c.Close()
// 阻塞主线程
down := make(chan byte)
// 启动一个线程,读取从服务端发送过来的数据
go func() {
for {
_, message, _ := c.ReadMessage()
fmt.Println("服务端回复:" + string(message))
}
}()
//启动一个线程输入消息
go func() {
for {
var input string
fmt.Scanln(&input)
c.WriteMessage(websocket.TextMessage, []byte(input))
}
}()
for {
<-down
}
}
Then start the service, connect to the server, and the words returned by the server indicate that the connection is successful
The client enters information in the console and clicks Enter
The server receives and prints the information sent by the client
Then enter the message in the server console and press Enter
The client receives the reply message from the server and the connection is successful
Since the address set above will change randomly, within 24 hours, we need to fix the tcp public network address (need to upgrade the professional package), open the official website www.cpolar.com, click Reserve, and then reserve the tcp address.
Copy the reserved address, open the web ui interface, update and add the reserved address information