github.com/gdamore/mangos@v1.4.0/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  	"nanomsg.org/go-mangos"
    21  	"nanomsg.org/go-mangos/protocol/sub"
    22  	"nanomsg.org/go-mangos/transport/ws"
    23  )
    24  
    25  // subClient implements the client for SUB.
    26  func subClient(port int) {
    27  	sock, err := sub.NewSocket()
    28  	if err != nil {
    29  		die("cannot make req socket: %v", err)
    30  	}
    31  	sock.AddTransport(ws.NewTransport())
    32  	if err = sock.SetOption(mangos.OptionSubscribe, []byte{}); err != nil {
    33  		die("cannot set subscription: %v", err)
    34  	}
    35  	url := fmt.Sprintf("ws://127.0.0.1:%d/sub", port)
    36  	if err = sock.Dial(url); err != nil {
    37  		die("cannot dial req url: %v", err)
    38  	}
    39  	if m, err := sock.Recv(); err != nil {
    40  		die("Cannot recv sub: %v", err)
    41  	} else {
    42  		fmt.Printf("%s\n", string(m))
    43  	}
    44  }