go.nanomsg.org/mangos/v3@v3.4.3-0.20240217232803-46464076f1f5/examples/websocket/subclient.go (about)

     1  // Copyright 2018 The Mangos Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use file except in compliance with the License.
     5  // You may obtain a copy of the license at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"go.nanomsg.org/mangos/v3"
    21  	"go.nanomsg.org/mangos/v3/protocol/sub"
    22  
    23  	// register ws transport
    24  	_ "go.nanomsg.org/mangos/v3/transport/ws"
    25  )
    26  
    27  // subClient implements the client for SUB.
    28  func subClient(port int) {
    29  	sock, err := sub.NewSocket()
    30  	if err != nil {
    31  		die("cannot make req socket: %v", err)
    32  	}
    33  	if err = sock.SetOption(mangos.OptionSubscribe, []byte{}); err != nil {
    34  		die("cannot set subscription: %v", err)
    35  	}
    36  	url := fmt.Sprintf("ws://127.0.0.1:%d/sub", port)
    37  	if err = sock.Dial(url); err != nil {
    38  		die("cannot dial req url: %v", err)
    39  	}
    40  	if m, err := sock.Recv(); err != nil {
    41  		die("Cannot recv sub: %v", err)
    42  	} else {
    43  		fmt.Printf("%s\n", string(m))
    44  	}
    45  }