tractor.dev/toolkit-go@v0.0.0-20241010005851-214d91207d07/duplex/mux/dial_ws.go (about)

     1  //go:build !tinygo
     2  
     3  package mux
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"golang.org/x/net/websocket"
     9  )
    10  
    11  // DialWS establishes a mux session via WebSocket connection.
    12  // The address must be a host and port. Opening a WebSocket
    13  // connection at a particular path is not supported.
    14  func DialWS(addr string) (Session, error) {
    15  	ws, err := websocket.Dial(fmt.Sprintf("ws://%s/", addr), "", fmt.Sprintf("http://%s/", addr))
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  	ws.PayloadType = websocket.BinaryFrame
    20  	return New(ws), nil
    21  }