1. Java 服務(wù)端demo環(huán)境
- jdk1.8
- 框架:springboot+maven
- 工具IDEA
2. 在pom文件引入第三包封裝的netty框架maven坐標(biāo)
<dependency>
<groupId>io.github.fzdwx</groupId>
<artifactId>sky-http-springboot-starter</artifactId>
<version>0.10.6</version>
</dependency>
注意:pom文件里需注釋掉springbootweb啟動(dòng)器,web啟動(dòng)器默認(rèn)是tomcat服務(wù)啟動(dòng),會(huì)和netty服務(wù)沖突
3. 創(chuàng)建服務(wù)端,以接口模式調(diào)用,方便外部調(diào)用
@GetMapping("/getConnect")
public void getConnect(HttpServerRequest request){
request.upgradeToWebSocket(ws -> {
ws.mountOpen(h->{
ws.send("連接成功,開(kāi)始聊天吧!");
});
ws.mountText(s -> {
System.out.println(s);
//對(duì)方回復(fù)
System.out.println("客戶(hù)端回復(fù): "+s);
//獲取控制臺(tái)輸入的值
Scanner scanner =new Scanner(System.in);
String next = scanner.next();
ws.send(next);
});
});
}
4. 啟動(dòng)服務(wù),出現(xiàn)以下信息表示啟動(dòng)成功,暴露端口默認(rèn)9999
5. 創(chuàng)建隧道映射內(nèi)網(wǎng)端口
這里我們用cpolar內(nèi)網(wǎng)穿透來(lái)映射內(nèi)網(wǎng)端口,它支持http/https/tcp協(xié)議,不限制流量,無(wú)需公網(wǎng)ip,也不用設(shè)置路由器,操作簡(jiǎn)單。
- cpolar一鍵安裝腳本:(國(guó)內(nèi)用戶(hù))
curl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bash
- 或短鏈接安裝方式:(國(guó)外用戶(hù))
curl -sL https://git.io/cpolar | sudo bash
- 查看cpolar版本信息
cpolar version
如果正常顯示,則安裝成功
- cpolar進(jìn)行token認(rèn)證
cpolar官網(wǎng):https://www.cpolar.com/
進(jìn)入cpolar官網(wǎng),注冊(cè)一個(gè)賬號(hào)并登錄進(jìn)入后臺(tái),點(diǎn)擊左側(cè)的驗(yàn)證
,可以查看到token碼,復(fù)制并執(zhí)行命令進(jìn)行認(rèn)證
cpolar authtoken xxxxxxxxxxxxxxxxxx
- 配置cpolar開(kāi)機(jī)自啟動(dòng)
sudo systemctl enable cpolar
- 守護(hù)進(jìn)程方式,啟動(dòng)cpolar
sudo systemctl start cpolar
- 查看cpolar守護(hù)進(jìn)程狀態(tài),如正常為active,則為正常啟動(dòng)狀態(tài)
sudo systemctl status cpolar
cpolar安裝成功后,默認(rèn)會(huì)配置兩個(gè)默認(rèn)隧道:一個(gè)ssh隧道和一個(gè)website隧道,可自行刪減或者修改。
接著把本地服務(wù)通過(guò)cpolar暴露到公網(wǎng),瀏覽器訪問(wèn)http://127.0.0.1:9200,登錄cpolar web ui 界面,創(chuàng)建一個(gè)tcp隧道,指向9999端口
注意:該隧道選擇的是臨時(shí)tcp地址和端口,24小時(shí)內(nèi)會(huì)變化,如需固定tcp地址,可升級(jí)為專(zhuān)業(yè)套餐做tcp地址固定!
6. 查看狀態(tài)->在線隧道,復(fù)制所創(chuàng)建隧道的公網(wǎng)地址加端口號(hào)
此時(shí),websocket服務(wù)端已經(jīng)從本地localhost暴露至公網(wǎng),接著我們創(chuàng)建一個(gè)客戶(hù)端測(cè)試公網(wǎng)訪問(wèn)socket服務(wù)端連接
7. 以基于go的socket客戶(hù)端為例,通過(guò)公網(wǎng)連接java socket服務(wù)端
- go版本:1.19
- 工具:vscode
8. 通過(guò)git下載websocket框架
go get github.com/gorilla/websocket
9. 創(chuàng)建客戶(hù)端, 注意:Host值為上面復(fù)制的隧道公網(wǎng)地址!!
package main
import (
"fmt"
"log"
"net/url"
"github.com/gorilla/websocket"
)
func main() {
// 定義服務(wù)端的地址
u := url.URL{
Scheme: "ws",
Host: "3.tcp.vip.cpolar.cn:10793", //地址為復(fù)制隧道的公網(wǎng)地址
Path: "/eth/getConnect"} //服務(wù)端controller 映射地址
// 與服務(wù)端建立連接
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial:", err)
}
defer c.Close()
// 阻塞主線程
down := make(chan byte)
// 啟動(dòng)一個(gè)線程,讀取從服務(wù)端發(fā)送過(guò)來(lái)的數(shù)據(jù)
go func() {
for {
_, message, _ := c.ReadMessage()
fmt.Println("服務(wù)端回復(fù):" + string(message))
}
}()
//啟動(dòng)一個(gè)線程輸入消息
go func() {
for {
var input string
fmt.Scanln(&input)
c.WriteMessage(websocket.TextMessage, []byte(input))
}
}()
for {
<-down
}
}
10. 接著啟動(dòng)服務(wù),與服務(wù)端連接,出現(xiàn)服務(wù)端返回的字樣表示連接成功
11. 客戶(hù)端在控制臺(tái)輸入信息,回車(chē)
12. 服務(wù)端出現(xiàn)客戶(hù)端發(fā)送的信息
13. 服務(wù)端控制臺(tái)輸入消息,回車(chē)
14. 客戶(hù)端收到服務(wù)端回復(fù)的消息,連接成功
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-762295.html
需要注意,免費(fèi)使用cpolar所生成的公網(wǎng)地址為隨機(jī)臨時(shí)地址,24小時(shí)內(nèi)會(huì)發(fā)生變化。如果需要長(zhǎng)期遠(yuǎn)程連接,建議為其配置固定的tcp端口地址。即登錄cpolar官網(wǎng)后,點(diǎn)擊預(yù)留,保留一個(gè)固定tcp端口地址,然后將其配置到相應(yīng)的隧道中即可。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-762295.html
到了這里,關(guān)于如何將本地websocket發(fā)布至公網(wǎng)并實(shí)現(xiàn)遠(yuǎn)程訪問(wèn)服務(wù)端的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!