survey

package
Version: 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: 6 Imported by: 1

README

mangosServer Survey

Example Code to start a survey server, send a survey, and receive a survey response. ###Server Code package main

import (
	"github.com/DanielRenne/mangosServer/survey"
	"log"
	"time"
)

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

func main() {
	var s survey.Server

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

	//Code a forever loop to stop main from exiting.
	for {
        time.Sleep(3 * time.Second)
        go s.Send([]byte("Sending Survey"))
	}

}

func handleSurveyResponse(msg []byte) {
	//Process Survey Results.
	log.Printf(string(msg))
}

###Client Code

package main

import (
	"github.com/go-mangos/mangos"
	"github.com/go-mangos/mangos/protocol/respondent"
	"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 makeSurveyConnection(url)

	for {

	}
}

func makeSurveyConnection(url string) {
	var err error
	var sock mangos.Socket

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

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

	log.Println("Connecting to Survey Server")

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

	go respondToSurvey(sock, "Hello Survey.")

}

//Responds to the Survey
func respondToSurvey(sock mangos.Socket, surveyResponse string) {
	var err error
	var msg []byte

	if msg, err = sock.Recv(); err != nil {
        log.Printf("Error Receiving at respondToSurvey:  %v", err.Error())
        return
	}

	log.Println(string(msg))

	if err = sock.Send([]byte(surveyResponse)); err != nil {
        log.Printf("Error Sending Survey Response at respondToSurvey:  %v", err.Error())
        return
	}

	go respondToSurvey(sock, "Hello Survey.")
}	

Documentation

Overview

Package survey supports the implementation of a survey server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ResponseHandler

type ResponseHandler func([]byte)

type Server

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

func (*Server) Listen

func (self *Server) Listen(url string, ms time.Duration, workers int, handler ResponseHandler) error

Starts a Survey on the specified url for a specified duration for clients to repsond. A set of workers can run to handle more traffic.

func (*Server) Send

func (self *Server) Send(payload []byte) error

Send the survey question to clients and set a channel of slice messages to process.

Source Files

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL