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 (
|
||||
"context"
|
||||
"net"
|
||||
"syscall/js"
|
||||
|
||||
"github.com/syumai/workers/cloudflare/internal/cfruntimecontext"
|
||||
"github.com/syumai/workers/internal/jsutil"
|
||||
)
|
||||
|
||||
type Dialer struct {
|
||||
connect js.Value
|
||||
opts *SocketOptions
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
type SocketOptions struct {
|
||||
SecureTransport string `json:"secureTransport"`
|
||||
AllowHalfOpen bool `json:"allowHalfOpen"`
|
||||
}
|
||||
|
||||
// NewDialer
|
||||
func NewDialer(ctx context.Context, options *SocketOptions) (*Dialer, error) {
|
||||
func Connect(ctx context.Context, addr string, opts *SocketOptions) (net.Conn, error) {
|
||||
connect, err := cfruntimecontext.GetRuntimeContextValue(ctx, "connect")
|
||||
if err != nil {
|
||||
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()
|
||||
if d.opts != nil {
|
||||
if d.opts.AllowHalfOpen {
|
||||
if opts != nil {
|
||||
if opts.AllowHalfOpen {
|
||||
optionsObj.Set("allowHalfOpen", true)
|
||||
}
|
||||
if d.opts.SecureTransport != "" {
|
||||
optionsObj.Set("secureTransport", d.opts.SecureTransport)
|
||||
if opts.SecureTransport != "" {
|
||||
optionsObj.Set("secureTransport", opts.SecureTransport)
|
||||
}
|
||||
}
|
||||
sock := &TCPSocket{}
|
||||
sock.socket = d.connect.Invoke(addr, optionsObj)
|
||||
sock.options = d.opts
|
||||
sock.init(d.ctx)
|
||||
sock.socket = connect.Invoke(addr, optionsObj)
|
||||
sock.options = opts
|
||||
sock.init(ctx)
|
||||
return sock, nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user