raw

package
v0.0.0-...-b09fe9f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2018 License: MIT Imports: 4 Imported by: 0

README

mangosServer Raw

Example Code to start a raw reply server, reply with a new message. The server will have 2 workers.

###Server Code package main

import (
	"github.com/DanielRenne/mangosServer/raw"
	"github.com/go-mangos/mangos"
	"log"
)

const url = "tcp://127.0.0.1:600"

func main() {
	var s raw.Server

	err := s.Listen(url, 2, handleRawRequest)
	if err != nil {
		log.Printf("Error:  %v", err.Error)
	}

	//Code a forever loop to stop main from exiting.
	for {

	}

}

func handleRawRequest(s *raw.Server, m *mangos.Message) {

	log.Printf(string(m.Body))
	m.Body = []byte("Custom Response to the Request")
	s.Reply(m)
}

###Client Code

package main

import (
	"github.com/go-mangos/mangos"
	"github.com/go-mangos/mangos/protocol/req"
	"github.com/go-mangos/mangos/transport/ipc"
	"github.com/go-mangos/mangos/transport/tcp"
	"log"
)

const url = "tcp://127.0.0.1:600"

func main() {

	go makeRequest(url, "Hello World")

	for {

	}
}

func makeRequest(url string, message string) {
	var sock mangos.Socket
	var err error

	if sock, err = req.NewSocket(); err != nil {
		log.Printf("Error creating new Socket at raw.TestSingleRawMessage:  %v", err.Error())
		return
	}

	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())

	log.Println("Connecting to Raw Server")

	if err = sock.Dial(url); err != nil {
		log.Printf("Error Dialing at raw.TestSingleRawMessage:  %v", err.Error())
		return
	}

	body := []byte(message)
	m := mangos.NewMessage(len(body) + 2)
	m.Body = body

	log.Println("Sending Request to Raw Server")
	if err = sock.SendMsg(m); err != nil {
		log.Printf("Error sending raw message at raw.TestSingleRawMessage:  %v", err.Error())
		return
	}

	respondToRaw(sock)

}

//Responds to the Raw Socket Message
func respondToRaw(sock mangos.Socket) {

	msg, err := sock.RecvMsg()

	if err != nil {
		return
	}

	log.Println(string(msg.Body))

}

Documentation

Overview

Package raw provides implementation of a raw mangos server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ResponseHandler

type ResponseHandler func(*Server, *mangos.Message)

type Server

type Server struct {
	// contains filtered or unexported fields
}

func (*Server) Listen

func (self *Server) Listen(url string, workers int, handler ResponseHandler) error

Starts a Raw Socket on the specified url. A set of workers can run to handle more traffic.

func (*Server) Reply

func (self *Server) Reply(m *mangos.Message) error

Reply to the Raw Request Message.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL