New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocket host and the restful host setup #1069

Open
jjhesk opened this Issue Aug 20, 2018 · 0 comments

Comments

Projects
None yet
1 participant
@jjhesk

jjhesk commented Aug 20, 2018

I have been trying setup both hosts for websocket and the restful http host. Are there such example for more references?

i used the websocket code and it is not working...

Error during WebSocket handshake: Unexpected response code: 200

func StartWebsocket(app *iris.Application) {
	ws := websocket.New(websocket.Config{
		ReadBufferSize:    1024,
		WriteBufferSize:   1024,
		EnableCompression: true,
	})
	ws.OnConnection(handleConnection)
	app.Get("/ws", ws.Handler())
	app.Any("/iris-ws.js", func(ctx iris.Context) {
		ctx.Write(websocket.ClientSource)
	})

	//app.NewHost(&http.Server{Addr:":8050"}).ListenAndServe()
}

func SetupWebClient() {
	app := iris.New()
	app.RegisterView(iris.HTML("./dist", ".html").Reload(false))
	app.OnErrorCode(iris.StatusInternalServerError, func(ctx iris.Context) {
		// .Values are used to communicate between handlers, middleware.
		errMessage := ctx.Values().GetString("error")
		if errMessage != "" {
			ctx.Writef("Internal server error: %s", errMessage)
			return
		}
		ctx.Writef("(Unexpected) internal server error")
	})

	app.Use(func(ctx iris.Context) {
		ctx.Application().Logger().Infof("Begin request for path: %s", ctx.Path())
		ctx.Next()
	})

	app.Get("/", routeHome)

	app.Get("/encode", func(ctx iris.Context) {
		doe := User{
			Username:  "Johndoe",
			Firstname: "John",
			Lastname:  "Doe",
			City:      "Neither FBI knows!!!",
			Age:       25,
		}

		ctx.JSON(doe)
	})

	apiRoute := app.Party("api.", logThisMiddleware)
	{
		// http://api.mydomain.com/users/42
		// Method GET: http://localhost:8080/users/42
		apiRoute.Get("/users/{id:int min(1)}", getUserByID)
		// Method POST: http://localhost:8080/users/create
		apiRoute.Post("/users/create", createUser)
		apiRoute.Any("/iris-ws.js", websocket.ClientHandler())
		app.Post("/decode", func(ctx iris.Context) {
			var user User
			ctx.ReadJSON(&user)
			ctx.Writef("%s %s is %d years old and comes from %s", user.Firstname, user.Lastname, user.Age, user.City)
		})
	}
	assetHandler := app.StaticHandler("./dist", false, false)
	app.SPA(assetHandler)


	go StartWebsocket(app)
	// Listen for incoming HTTP/1.x & HTTP/2 clients on localhost port 8080.
	app.Run(iris.Addr(":8001"), iris.WithCharset("UTF-8"), iris.WithoutVersionChecker)

	//go statics.Run(iris.Addr(":8002"))

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment