Пример #1
0
func main() {
	flag.Parse()
	if *runMode == "server" {

		s := turnpike.NewServer(false)

		http.Handle("/ws", s.Handler)
		err := http.ListenAndServe(":8080", nil)
		if err != nil {
			panic("ListenAndServe: " + err.Error())
		}

	} else if *runMode == "client" {
		c := turnpike.NewClient()
		err := c.Connect("ws://127.0.0.1:8080/ws", "http://localhost/")
		if err != nil {
			panic("Error connecting:" + err.Error())
		}

		c.Subscribe("event:test", testHandler)

		for {
			c.Publish("event:test", "test")
			<-time.After(time.Second)
		}
	} else {
		fmt.Printf("runMode must be one of server or client, you passed %s", runMode)
	}
}
Пример #2
0
func main() {
	flag.Parse()
	if *runMode == "server" {
		s := turnpike.NewServer(false)
		s.RegisterRPC("rpc:test", handleTest)

		http.Handle("/ws", s.Handler)
		err := http.ListenAndServe(":8080", nil)
		if err != nil {
			panic("ListenAndServe: " + err.Error())
		}

	} else if *runMode == "client" {
		c := turnpike.NewClient()
		err := c.Connect("ws://127.0.0.1:8080/ws", "http://localhost/")
		if err != nil {
			panic("Error connecting:" + err.Error())
		}

		resultCh := c.Call("rpc:test")
		result := <-resultCh
		fmt.Printf("Call result is: %s\n", result.Result)
	} else {
		fmt.Printf("runMode must be one of server or client, you passed %s", runMode)
	}
}
Пример #3
0
func ExampleClient_NewClient() {
	c := turnpike.NewClient()
	err := c.Connect("ws://127.0.0.1:8080/ws", "http://localhost/")
	if err != nil {
		panic("Error connecting:" + err.Error())
	}

	c.Call("rpc:test")
}
Пример #4
0
func main() {
	c := turnpike.NewClient()
	fmt.Print("Server address (default: localhost:9091/ws)\n> ")
	read := bufio.NewReader(os.Stdin)
	// if _, err := fmt.Scanln(&server); err != nil {
	server, err := read.ReadString('\n')
	if err != nil {
		fmt.Println("Error reading from stdin:", err)
		return
	}
	server = strings.TrimSpace(server)
	if server == "" {
		server = "localhost:9091/ws"
	}
	if err := c.Connect("ws://"+server, "http://localhost:8070"); err != nil {
		fmt.Println("Error connecting:", err)
		return
	}

	fmt.Print(
		"----------------------------------------------------------------------\n",
		"Connected to server at: ", server, "\n",
		"With session id: ", c.SessionId, "\n",
		"Enter WAMP message, parameters separated by spaces\n",
		"PREFIX=1, CALL=2, SUBSCRIBE=5, UNSUBSCRIBE=6, PUBLISH=7\n",
		"----------------------------------------------------------------------\n")

	for {
		fmt.Print(c.ServerIdent, "> ")
		// if _, err := fmt.Scanln(&msgType, &args); err != nil {
		line, err := read.ReadString('\n')
		if err != nil {
			fmt.Println("Error reading from stdin:", err)
			return
		}
		line = strings.TrimSpace(line)
		if line == "" {
			continue
		}
		// fmt.Println(line)

		// get the type
		params := strings.SplitN(line, " ", 2)
		line = params[1]
		msgType, err := strconv.Atoi(params[0])
		if err != nil {
			fmt.Println("Error parsing message type:", params[0])
			continue
		}

		err = nil
		switch msgType {
		case PREFIX:
			var prefix, URI string
			fmt.Sscan(line, &prefix, &URI)
			err = c.Prefix(prefix, URI)
		case CALL:
			args := strings.Split(line, " ")
			resultCh := make(chan turnpike.CallResult)
			resultCh = c.Call(args[0], args[1:])
			r := <-resultCh
			if r.Error != nil {
				fmt.Println(r.Error)
			} else {
				fmt.Printf("Result %s\n", r.Result)
			}
		case SUBSCRIBE:
			eventCh := make(chan interface{})
			err = c.Subscribe(line, func(uri string, event interface{}) {
				eventCh <- event
			})
			if err != nil {
				fmt.Printf("Failed to subscribe :%v", err)
			}
			go receive(eventCh)
		case UNSUBSCRIBE:
			err = c.Unsubscribe(line)
		case PUBLISH:
			args := strings.Split(line, " ")
			if len(args) > 2 {
				err = c.Publish(args[0], args[1], args[2:])
			} else {
				err = c.Publish(args[0], args[1])
			}
			if err != nil {
				fmt.Printf("Failed to publish :%v", err)
			}
		default:
			fmt.Println("Invalid message type:", msgType)
			continue
		}

		if err != nil {
			fmt.Println("Error sending message:", err)
		}
	}
}