示例#1
0
文件: demo.go 项目: vanishs/gwsrpc
//Start Start
func Start(port int, tls bool, certFile, keyFile string, ch chan string) {

	log.Println("demo start")
	sid = utils.Randstr20()

	s := new(grpcService)
	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
	if err != nil {
		grpclog.Fatalf("failed to listen: %v", err)
	}
	var opts []grpc.ServerOption
	if tls {
		creds, err := credentials.NewServerTLSFromFile(certFile, keyFile)
		if err != nil {
			grpclog.Fatalf("Failed to generate credentials %v", err)
		}
		opts = []grpc.ServerOption{grpc.Creds(creds)}
	}

	grpcServer := grpc.NewServer(opts...)
	demo.RegisterRPCServer(grpcServer, s)
	go func() {
		<-ch
		grpcServer.Stop()
	}()
	grpcServer.Serve(lis)

	log.Println("demo end")
}
示例#2
0
//Gain Gateway Main function is Gain
func Gain(
	_aaaAddr string,
	_aaaTLS bool,
	_aaaFile string,
	_aaaOver string,

	_bbbAddr string,
	_bbbTLS bool,
	_bbbFile string,
	_bbbOver string,

	_demoAddr string,
	_demoTLS bool,
	_demoFile string,
	_demoOver string,

	_localaddr string,
	_localsslname string,
) {
	pp, _ := io.Pipe()
	log.Println("Gateway start.", proto.GoGoProtoPackageIsVersion2, pp)
	localaddr = _localaddr

	aaaAddr = _aaaAddr
	aaaTLS = _aaaTLS
	aaaFile = _aaaFile
	aaaOver = _aaaOver

	bbbAddr = _bbbAddr
	bbbTLS = _bbbTLS
	bbbFile = _bbbFile
	bbbOver = _bbbOver

	demoAddr = _demoAddr
	demoTLS = _demoTLS
	demoFile = _demoFile
	demoOver = _demoOver

	log.Println("run Gain")

	go h.run()

	gwid := utils.Randstr20()

	aaaConn(gwid)
	bbbConn(gwid)
	demoConn(gwid)
	http.HandleFunc("/", ws.ServeWs)

	genws.SetHandleConnect(1024*1024*1, 256, 10, 60, rpcmainOnConnect)

	var err error
	if _localsslname == "" {
		err = http.ListenAndServe(localaddr, nil)
	} else {
		err = http.ListenAndServeTLS(localaddr, _localsslname+".pem", _localsslname+".key", nil)
	}

	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}

}