mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
rename NewDialer and Dial to Connect
This commit is contained in:
parent
258911a292
commit
653b0d17aa
@ -3,50 +3,33 @@ package sockets
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
"syscall/js"
|
|
||||||
|
|
||||||
"github.com/syumai/workers/cloudflare/internal/cfruntimecontext"
|
"github.com/syumai/workers/cloudflare/internal/cfruntimecontext"
|
||||||
"github.com/syumai/workers/internal/jsutil"
|
"github.com/syumai/workers/internal/jsutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Dialer struct {
|
|
||||||
connect js.Value
|
|
||||||
opts *SocketOptions
|
|
||||||
ctx context.Context
|
|
||||||
}
|
|
||||||
|
|
||||||
type SocketOptions struct {
|
type SocketOptions struct {
|
||||||
SecureTransport string `json:"secureTransport"`
|
SecureTransport string `json:"secureTransport"`
|
||||||
AllowHalfOpen bool `json:"allowHalfOpen"`
|
AllowHalfOpen bool `json:"allowHalfOpen"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDialer
|
func Connect(ctx context.Context, addr string, opts *SocketOptions) (net.Conn, error) {
|
||||||
func NewDialer(ctx context.Context, options *SocketOptions) (*Dialer, error) {
|
|
||||||
connect, err := cfruntimecontext.GetRuntimeContextValue(ctx, "connect")
|
connect, err := cfruntimecontext.GetRuntimeContextValue(ctx, "connect")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &Dialer{connect: connect, opts: options, ctx: ctx}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Dialer) Dial(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
||||||
switch network {
|
|
||||||
case "tcp":
|
|
||||||
default:
|
|
||||||
panic("not implemented")
|
|
||||||
}
|
|
||||||
optionsObj := jsutil.NewObject()
|
optionsObj := jsutil.NewObject()
|
||||||
if d.opts != nil {
|
if opts != nil {
|
||||||
if d.opts.AllowHalfOpen {
|
if opts.AllowHalfOpen {
|
||||||
optionsObj.Set("allowHalfOpen", true)
|
optionsObj.Set("allowHalfOpen", true)
|
||||||
}
|
}
|
||||||
if d.opts.SecureTransport != "" {
|
if opts.SecureTransport != "" {
|
||||||
optionsObj.Set("secureTransport", d.opts.SecureTransport)
|
optionsObj.Set("secureTransport", opts.SecureTransport)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sock := &TCPSocket{}
|
sock := &TCPSocket{}
|
||||||
sock.socket = d.connect.Invoke(addr, optionsObj)
|
sock.socket = connect.Invoke(addr, optionsObj)
|
||||||
sock.options = d.opts
|
sock.options = opts
|
||||||
sock.init(d.ctx)
|
sock.init(ctx)
|
||||||
return sock, nil
|
return sock, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user