修复依赖

This commit is contained in:
2025-10-15 14:19:16 +08:00
parent c52d5359e9
commit 865d6fac65
9 changed files with 47 additions and 91 deletions

View File

@@ -53,7 +53,6 @@ var (
"g,gateway": true,
"a,action": true,
"t,target": true,
"m,message": true,
})
// 获取运行模式参数
@@ -78,12 +77,17 @@ var (
wsStr := "ws://192.168.50.173:51888/ws"
err = service.P2P().Start(ctx, wsStr)
case "dht":
h, _ := service.P2P().CreateLibp2pHost(ctx, 0)
service.P2P().DHTStart(ctx, h)
default:
// 显示帮助信息
g.Log().Info(ctx, p2pHelpDescription)
}
if err != nil {
return err
}
return
},
}

View File

@@ -1,5 +0,0 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package hello

View File

@@ -1,16 +0,0 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package hello
import (
"github.com/ayflying/p2p/api/hello"
)
type ControllerV1 struct{}
func NewV1() hello.IHelloV1 {
return &ControllerV1{}
}

View File

@@ -1,13 +0,0 @@
package hello
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/ayflying/p2p/api/hello/v1"
)
func (c *ControllerV1) Hello(ctx context.Context, req *v1.HelloReq) (res *v1.HelloRes, err error) {
g.RequestFromCtx(ctx).Response.Writeln("Hello World!")
return
}

View File

@@ -34,7 +34,7 @@ type Client struct {
func (s *sP2P) Start(ctx context.Context, wsStr string) (err error) {
hostObj, err := s.createLibp2pHost(ctx, 0)
hostObj, err := s.CreateLibp2pHost(ctx, 0)
if err != nil {
g.Log().Error(ctx, err)
}
@@ -70,7 +70,7 @@ func (s *sP2P) Start(ctx context.Context, wsStr string) (err error) {
}
// 创建libp2p主机
func (s *sP2P) createLibp2pHost(ctx context.Context, port int) (host.Host, error) {
func (s *sP2P) CreateLibp2pHost(ctx context.Context, port int) (host.Host, error) {
if port == 0 {
//port = grand.N(50000, 55000)
port = 53533

View File

@@ -9,7 +9,7 @@ import (
)
// 初始化无服务器DHT作为节点加入DHT网络
func (s *sP2P) InitDHT(ctx context.Context, h host.Host) (*dht.IpfsDHT, error) {
func (s *sP2P) DHTStart(ctx context.Context, h host.Host) (*dht.IpfsDHT, error) {
// 创建DHT实例设置为“客户端+服务端模式”(既可以查找数据,也可以存储数据)
kdht, err := dht.New(ctx, h, dht.Mode(dht.ModeServer))
if err != nil {

View File

@@ -9,15 +9,21 @@ import (
"context"
"github.com/gogf/gf/v2/net/ghttp"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/host"
)
type (
IP2P interface {
Start(ctx context.Context, wsStr string) (err error)
// 创建libp2p主机
CreateLibp2pHost(ctx context.Context, port int) (host.Host, error)
// 发现并连接目标节点
DiscoverAndConnect(targetID string) error
// 发送数据到目标节点
SendData(targetID string, data []byte) error
// 初始化无服务器DHT作为节点加入DHT网络
DHTStart(ctx context.Context, h host.Host) (*dht.IpfsDHT, error)
GatewayStart(ctx context.Context, group *ghttp.RouterGroup) (err error)
}
)