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