本文整理匯總了Golang中github.com/gdamore/mangos/protocol/rep.NewSocket函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewSocket函數的具體用法?Golang NewSocket怎麽用?Golang NewSocket使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewSocket函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestMaxRxSizeSet
func TestMaxRxSizeSet(t *testing.T) {
srep, err := rep.NewSocket()
if err != nil {
t.Errorf("Failed to make REP: %v", err)
return
}
defer srep.Close()
err = srep.SetOption(mangos.OptionMaxRecvSize, 100)
if err != nil {
t.Errorf("Failed SetOption: %v", err)
return
}
v, err := srep.GetOption(mangos.OptionMaxRecvSize)
if err != nil {
t.Errorf("Failed GetOption: %v", err)
return
}
if val, ok := v.(int); !ok {
t.Errorf("Returned value not type int")
} else if val != 100 {
t.Errorf("Returned value %d not %d", val, 100)
}
}
示例2: StartNanomsgJsonServer
func StartNanomsgJsonServer(port string) {
var server mangos.Socket
var err error
if server, err = rep.NewSocket(); err != nil {
log.Fatal(err)
}
server.AddTransport(tcp.NewTransport())
server.Listen("tcp://0.0.0.0:" + port)
for {
msg, _ := server.Recv()
var body Request
var response []byte
json.Unmarshal(msg, &body)
if response, err = json.Marshal(Response{Method: body.Method, PayloadLength: len(body.Payload)}); err != nil {
log.Fatal(err)
}
server.Send(response)
}
}
示例3: NewSocket
// NewSocket allocates a new Socket. The Socket is the handle used to
// access the underlying library.
func NewSocket(d Domain, p Protocol) (*Socket, error) {
var s Socket
var err error
s.proto = p
s.dom = d
switch p {
case PUB:
s.sock, err = pub.NewSocket()
case SUB:
s.sock, err = sub.NewSocket()
case PUSH:
s.sock, err = push.NewSocket()
case PULL:
s.sock, err = pull.NewSocket()
case REQ:
s.sock, err = req.NewSocket()
case REP:
s.sock, err = rep.NewSocket()
case SURVEYOR:
s.sock, err = surveyor.NewSocket()
case RESPONDENT:
s.sock, err = respondent.NewSocket()
case PAIR:
s.sock, err = pair.NewSocket()
case BUS:
s.sock, err = bus.NewSocket()
default:
err = mangos.ErrBadProto
}
if err != nil {
return nil, err
}
switch d {
case AF_SP:
case AF_SP_RAW:
err = s.sock.SetOption(mangos.OptionRaw, true)
default:
err = errBadDomain
}
if err != nil {
s.sock.Close()
return nil, err
}
// Compat mode sockets should timeout on send if we don't have any pipes
if err = s.sock.SetOption(mangos.OptionWriteQLen, 0); err != nil {
s.sock.Close()
return nil, err
}
s.rto = -1
s.sto = -1
all.AddTransports(s.sock)
return &s, nil
}
示例4: TestWebsockPath
func TestWebsockPath(t *testing.T) {
sockReq, _ := req.NewSocket()
sockRep, _ := rep.NewSocket()
tran := NewTransport()
l, e := tran.NewListener("ws://127.0.0.1:3335/mysock", sockReq)
if e != nil {
t.Errorf("Failed new Listener: %v", e)
return
}
d, e := tran.NewDialer("ws://127.0.0.1:3335/boguspath", sockRep)
if e != nil {
t.Errorf("Failed new Dialer: %v", e)
return
}
if e = l.Listen(); e != nil {
t.Errorf("Listen failed")
return
}
defer l.Close()
p, e := d.Dial()
if p != nil {
defer p.Close()
}
if e == nil {
t.Errorf("Dial passed, when should not have!")
return
}
t.Logf("Got expected error %v", e)
}
示例5: server
func server(url string, nworkers int) {
var sock mangos.Socket
var err error
var wg sync.WaitGroup
rand.Seed(time.Now().UnixNano())
if sock, err = rep.NewSocket(); err != nil {
die("can't get new rep socket: %s", err)
}
if err = sock.SetOption(mangos.OptionRaw, true); err != nil {
die("can't set raw mode: %s", err)
}
sock.AddTransport(ipc.NewTransport())
sock.AddTransport(tcp.NewTransport())
if err = sock.Listen(url); err != nil {
die("can't listen on rep socket: %s", err.Error())
}
wg.Add(nworkers)
fmt.Printf("Starting %d workers\n", nworkers)
for id := 0; id < nworkers; id++ {
go func(id int) {
defer wg.Done()
serverWorker(sock, id)
}(id)
}
wg.Wait()
}
示例6: StartNanomsgProtoServer
func StartNanomsgProtoServer(port string) {
var server mangos.Socket
var err error
if server, err = rep.NewSocket(); err != nil {
log.Fatal(err)
}
server.AddTransport(tcp.NewTransport())
server.Listen("tcp://0.0.0.0:" + port)
for {
msg, _ := server.Recv()
var body pb.Request
proto.Unmarshal(msg, &body)
data, _ := proto.Marshal(&pb.Response{
Method: body.Method,
PayloadLength: int64(len(body.Payload)),
})
server.Send(data)
}
}
示例7: TestWebsockMux
func TestWebsockMux(t *testing.T) {
sockReq, _ := req.NewSocket()
sockRep, _ := rep.NewSocket()
tran := NewTransport()
l, e := tran.NewListener("ws://127.0.0.1:3336/mysock", sockReq)
if e != nil {
t.Errorf("Failed new Listener: %v", e)
return
}
muxi, e := l.GetOption(OptionWebSocketMux)
if e != nil {
t.Errorf("Failed get mux: %v", e)
}
mux := muxi.(*http.ServeMux)
mux.HandleFunc("/bogus", bogusHandler)
d, e := tran.NewDialer("ws://127.0.0.1:3336/bogus", sockRep)
if e != nil {
t.Errorf("Failed new Dialer: %v", e)
return
}
if e = l.Listen(); e != nil {
t.Errorf("Listen failed")
return
}
defer l.Close()
p, e := d.Dial()
if p != nil {
defer p.Close()
}
if e == nil {
t.Errorf("Dial passed, when should not have!")
return
}
t.Logf("Got expected error %v", e)
// Now let's try to use http client.
resp, err := http.Get("http://127.0.0.1:3336/bogus")
if err != nil {
t.Errorf("Get of boguspath failed: %v", err)
return
}
if resp.StatusCode != 200 {
t.Errorf("Response code wrong: %d", resp.StatusCode)
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("ReadAll Failed: %v", err)
return
}
if string(body) != bogusstr {
t.Errorf("Results mismatch: %s != %s", string(body), bogusstr)
}
t.Logf("Got body: %s", string(body))
}
示例8: node0
func node0(url string) {
var sock mangos.Socket
var err error
var msg []byte
if sock, err = rep.NewSocket(); err != nil {
die("can't get new rep socket: %s", err)
}
sock.AddTransport(ipc.NewTransport())
sock.AddTransport(tcp.NewTransport())
if err = sock.Listen(url); err != nil {
die("can't listen on rep socket: %s", err.Error())
}
for {
// Could also use sock.RecvMsg to get header
msg, err = sock.Recv()
if string(msg) == "DATE" { // no need to terminate
fmt.Println("NODE0: RECEIVED DATE REQUEST")
d := date()
fmt.Printf("NODE0: SENDING DATE %s\n", d)
err = sock.Send([]byte(d))
if err != nil {
die("can't send reply: %s", err.Error())
}
}
}
}
示例9: NewDaemon
// NewDaemon creates and returns a new Daemon from the provided Config. An
// error is returned if the Daemon cannot be created.
func NewDaemon(config *Config) (*Daemon, error) {
rep, err := rep.NewSocket()
if err != nil {
return nil, err
}
rep.AddTransport(tcp.NewTransport())
return &Daemon{rep, nil, []*publisher{}, []*subscriber{}, config}, nil
}
示例10: Init
func (rt *repTest) Init(t *testing.T, addr string) bool {
var err error
if rt.Sock, err = rep.NewSocket(); err != nil {
rt.Errorf("NewSocket(): %v", err)
return false
}
return rt.T.Init(t, addr)
}
示例11: NewTranTest
func NewTranTest(tran mangos.Transport, addr string) *TranTest {
tt := &TranTest{addr: addr, tran: tran}
if strings.HasPrefix(tt.addr, "tls+tcp://") || strings.HasPrefix(tt.addr, "wss://") {
tt.cliCfg, _ = GetTlsConfig(false)
tt.srvCfg, _ = GetTlsConfig(true)
}
tt.sockRep, _ = rep.NewSocket()
tt.sockReq, _ = req.NewSocket()
return tt
}
示例12: TestTtlInvalidTooBig
func TestTtlInvalidTooBig(t *testing.T) {
srep, err := rep.NewSocket()
if err != nil {
t.Errorf("Failed to make REP: %v", err)
return
}
defer srep.Close()
err = srep.SetOption(mangos.OptionTtl, 256)
switch err {
case mangos.ErrBadValue: // expected result
case nil:
t.Errorf("Negative test fail, permitted too large TTL")
default:
t.Errorf("Negative test fail (256), wrong error %v")
}
}
示例13: TestMaxRxSizeInvalidNotInt
func TestMaxRxSizeInvalidNotInt(t *testing.T) {
srep, err := rep.NewSocket()
if err != nil {
t.Errorf("Failed to make REP: %v", err)
return
}
defer srep.Close()
err = srep.SetOption(mangos.OptionMaxRecvSize, "garbage")
switch err {
case mangos.ErrBadValue: // expected result
case nil:
t.Errorf("Negative test fail, permitted non-int value")
default:
t.Errorf("Negative test fail (garbage), wrong error %v")
}
}
示例14: newCmdServer
func newCmdServer(addr string) (mangos.Socket, error) {
log.Println("alauncher: creating cmd server...")
socket, err := rep.NewSocket()
if err != nil {
return nil, err
}
if err := socket.SetOption(mangos.OptionRecvDeadline, time.Second*3); err != nil {
socket.Close()
return nil, err
}
//socket.AddTransport(ipc.NewTransport())
socket.AddTransport(tcp.NewTransport())
if err := socket.Listen(addr); err != nil {
socket.Close()
return nil, err
}
return socket, nil
}
示例15: Receive
func (receiver *SPReceiver) Receive(address string) error {
var sock mangos.Socket
var err error
if sock, err = rep.NewSocket(); err != nil {
return err
}
sock.AddTransport(ipc.NewTransport())
sock.AddTransport(tcp.NewTransport())
if err := sock.Listen(address); err != nil {
return err
}
for {
if _, err := sock.Recv(); err != nil {
return err
}
}
return nil
}