我尝试使用https://github.com/sagiegurari/node-go-require和https://github.com/gopherjs将go文件作为模块运行
我的go代码中没有任何错误,但当使用下面的JS时,我遇到了“Error: Failed to convert Go file to JS”。
下面是我的nodeJS代码:
require('node-go-require');
//Failing here
const Phantom = require(__dirname + '/source/cmd/phantom.go').phantom;
var params = {
server:null,
boundIP:null,
boundPort:null,
timeOut:null
}
//Some other code here to set params
function start(){
//determine which parameters have been set by user
var args = [params.server];
if(params.boundIP != null){
args.push(params.boundIP);
} else {
args.push("0.0.0.0");
}
if(params.boundPort != null){
args.push(params.boundPort);
} else {
args.push(0);
}
if(params.timeOut != null){
args.push(params.timeOut);
} else {
args.push(60);
}
var p = Phantom.new(...args);
}下面是我的go主文件:
package main
import (
"github.com/gopherjs/gopherjs/js"
"github.com/jhead/phantom/internal/proxy"
)
func main() {
js.Module.Get("exports").Set("phantom", map[string]interface{}{
"new": proxy.New,
})
}下面是proxy.New函数:
package proxy
import (
"fmt"
"math/rand"
"net"
"time"
"github.com/gopherjs/gopherjs/js"
"github.com/jhead/phantom/internal/clientmap"
"github.com/jhead/phantom/internal/logging"
"github.com/jhead/phantom/internal/proto"
"github.com/tevino/abool"
reuse "github.com/libp2p/go-reuseport"
)
var idleCheckInterval = 5 * time.Second
type ProxyServer struct {
bindAddress *net.UDPAddr
remoteServerAddress *net.UDPAddr
pingServer net.PacketConn
server *net.UDPConn
clientMap *clientmap.ClientMap
prefs ProxyPrefs
dead *abool.AtomicBool
}
type ProxyPrefs struct {
BindAddress string
BindPort uint16
RemoteServer string
IdleTimeout time.Duration
}
func New(BindAddress string, BindPort uint16, RemoteServer string, IdleTimeout time.Duration) *js.Object {
var prefs = new(ProxyPrefs)
prefs.BindAddress = BindAddress
prefs.BindPort = BindPort
prefs.RemoteServer = RemoteServer
prefs.IdleTimeout = time.Duration(IdleTimeout) * time.Second
bindPort := prefs.BindPort
// Randomize port if not provided
if bindPort == 0 {
randSource := rand.NewSource(time.Now().UnixNano())
bindPort = (uint16(randSource.Int63()) % 14000) + 50000
}
// Format full bind address with port
prefs.BindAddress = fmt.Sprintf("%s:%d", prefs.BindAddress, bindPort)
bindAddress, err := net.ResolveUDPAddr("udp", prefs.BindAddress)
if err != nil {
return nil
}
remoteServerAddress, err := net.ResolveUDPAddr("udp", prefs.RemoteServer)
if err != nil {
return nil
}
return js.MakeWrapper(&ProxyServer{
bindAddress,
remoteServerAddress,
nil,
nil,
clientmap.New(prefs.IdleTimeout, idleCheckInterval),
*prefs,
abool.New(),
})
}如果有人能帮上忙,我将不胜感激,因为我几乎是新手。
下面是我为上下文所做的工作:https://github.com/OliverBrotchie/phantom
发布于 2020-04-08 15:15:31
您应该手动运行gopherjs以查看实际错误:例如:
/workspace/go/bin/gopherjs build /workspace/phantom/source/cmd/phantom.go例如,一个问题可能是go运行时太旧或太新。一旦你看到这个问题,下一个需要帮助的地方很可能是https://github.com/gopherjs/gopherjs上的gopherjs github项目页面,在那里你可以直接打开问题。
由于no- go -require只是它的一个代理(基本上是为了支持go文件的无缝节点请求功能),所以我不会试图找出问题所在。
https://stackoverflow.com/questions/61090649
复制相似问题