github.com/gdamore/mangos@v1.4.0/examples/websocket/reqclient.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  	"time"
    20  
    21  	"nanomsg.org/go-mangos/protocol/req"
    22  	"nanomsg.org/go-mangos/transport/ws"
    23  )
    24  
    25  // reqClient implements the client for REQ.
    26  func reqClient(port int) {
    27  	sock, e := req.NewSocket()
    28  	if e != nil {
    29  		die("cannot make req socket: %v", e)
    30  	}
    31  	sock.AddTransport(ws.NewTransport())
    32  	url := fmt.Sprintf("ws://127.0.0.1:%d/req", port)
    33  	if e = sock.Dial(url); e != nil {
    34  		die("cannot dial req url: %v", e)
    35  	}
    36  	// Time for TCP connection set up
    37  	time.Sleep(time.Millisecond * 10)
    38  	if e = sock.Send([]byte("Hello")); e != nil {
    39  		die("Cannot send req: %v", e)
    40  	}
    41  	if m, e := sock.Recv(); e != nil {
    42  		die("Cannot recv reply: %v", e)
    43  	} else {
    44  		fmt.Printf("%s\n", string(m))
    45  	}
    46  }