connect

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2020 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Copyright 2019-2020 Axetroy. All rights reserved. MIT license.

Copyright 2019-2020 Axetroy. All rights reserved. MIT license.

Copyright 2019-2020 Axetroy. All rights reserved. MIT license.

Copyright 2019-2020 Axetroy. All rights reserved. MIT license.

Copyright 2019-2020 Axetroy. All rights reserved. MIT license.

Copyright 2019-2020 Axetroy. All rights reserved. MIT license.

Copyright 2019-2020 Axetroy. All rights reserved. MIT license.

Copyright 2019-2020 Axetroy. All rights reserved. MIT license.

Copyright 2019-2020 Axetroy. All rights reserved. MIT license.

Copyright 2019-2020 Axetroy. All rights reserved. MIT license.

Index

Constants

This section is empty.

Variables

View Source
var UserRouter = router.Handler(func(c router.Context) {
	var (
		client *ws.Client
	)
	upgrader := websocket.Upgrader{
		CheckOrigin: func(r *http.Request) bool {
			return true
		},
	}

	webscoket, err := upgrader.Upgrade(c.Writer(), c.Request(), nil)

	if err != nil {
		c.ResponseFunc(nil, func() schema.Response {
			return schema.Response{
				Message: http.StatusText(http.StatusInternalServerError),
				Status:  schema.StatusFail,
				Data:    nil,
			}
		})
		return
	}

	defer func() {
		if r := recover(); r != nil {
			switch t := r.(type) {
			case string:
				err = errors.New(t)
			case error:
				err = t
			default:
				err = exception.Unknown
			}

			fmt.Printf("%+v\n", err)
		}

		_ = webscoket.Close()
		if client != nil {
			waiterId := ws.MatcherPool.GetMyWaiter(client.UUID)

			if waiterId != nil {
				waiterClient := ws.WaiterPoll.Get(*waiterId)

				_ = waiterClient.WriteJSON(ws.Message{
					From:    client.UUID,
					To:      *waiterId,
					Type:    string(ws.TypeResponseWaiterDisconnected),
					Payload: nil,
				})
			}

			ws.UserPoll.Remove(client.UUID)

			ws.MatcherPool.Leave(client.UUID)

			ws.MatcherPool.Broadcast <- true
		}
	}()

	client = ws.NewClient(webscoket)

	ws.UserPoll.Add(client)

	for {
		var msg ws.Message

		err := webscoket.ReadJSON(&msg)

		if err != nil {
			_ = client.WriteError(exception.InvalidParams.New(err.Error()), msg)
			continue
		}

		if err := validator.ValidateStruct(msg); err != nil {
			_ = client.WriteError(exception.InvalidParams.New(err.Error()), msg)
			continue
		}

	typeCondition:
		switch ws.TypeRequestUser(msg.Type) {

		case ws.TypeRequestUserAuth:
			if err := userTypeAuthHandler(client, msg); err != nil {
				_ = client.WriteError(err, msg)
			}

			break typeCondition

		case ws.TypeRequestUserConnect:
			if err := userTypeConnectHandler(client, msg); err != nil {
				_ = client.WriteError(err, msg)
			}
			break typeCondition

		case ws.TypeRequestUserDisconnect:
			if err := userTypeDisconnectHandler(client); err != nil {
				_ = client.WriteError(err, msg)
			}
			break typeCondition

		case ws.TypeRequestUserMessageText:
			if err := userTypeMessageHandler(client, msg); err != nil {
				_ = client.WriteError(err, msg)
			}
			break typeCondition
		default:
			_ = client.WriteError(exception.InvalidParams.New("未知的消息类型"), msg)
			break typeCondition
		}
	}
})
View Source
var WaiterRouter = router.Handler(func(c router.Context) {
	var (
		client *ws.Client
	)
	upgrader := websocket.Upgrader{
		CheckOrigin: func(r *http.Request) bool {
			return true
		},
	}

	webscoket, err := upgrader.Upgrade(c.Writer(), c.Request(), nil)

	if err != nil {
		c.ResponseFunc(nil, func() schema.Response {
			return schema.Response{
				Message: http.StatusText(http.StatusInternalServerError),
				Status:  schema.StatusFail,
				Data:    nil,
			}
		})
		return
	}

	defer func() {
		if r := recover(); r != nil {
			switch t := r.(type) {
			case string:
				err = errors.New(t)
			case error:
				err = t
			default:
				err = exception.Unknown
			}

			fmt.Printf("%+v\n", err)
		}

		_ = webscoket.Close()
		if client != nil {

			ws.WaiterPoll.Remove(client.UUID)

			users := ws.MatcherPool.GetMyUsers(client.UUID)

			for _, user := range users {
				userSocket := ws.UserPoll.Get(user)
				if userSocket != nil {
					_ = userSocket.WriteJSON(ws.Message{
						From:    client.UUID,
						To:      user,
						Type:    string(ws.TypeResponseUserDisconnected),
						Payload: nil,
					})
				}
			}

			ws.MatcherPool.RemoveWaiter(client.UUID)

			ws.MatcherPool.Broadcast <- true
		}
	}()

	client = ws.NewClient(webscoket)

	ws.WaiterPoll.Add(client)

	for {
		var msg ws.Message

		err := webscoket.ReadJSON(&msg)

		if err != nil {
			_ = client.WriteError(exception.InvalidParams.New(err.Error()), msg)
			continue
		}

		if err := validator.ValidateStruct(msg); err != nil {
			_ = client.WriteError(exception.InvalidParams.New(err.Error()), msg)
			continue
		}

	typeCondition:
		switch ws.TypeRequestWaiter(msg.Type) {
		case ws.TypeRequestWaiterAuth:
			if er := waiterTypeAuthHandler(client, msg); er != nil {
				_ = client.WriteError(er, msg)
			}
			break typeCondition
		case ws.TypeRequestWaiterReady:
			if er := waiterTypeReadyHandler(client); er != nil {
				_ = client.WriteError(er, msg)
			}
			break typeCondition
		case ws.TypeRequestWaiterDisconnect:
			if er := waiterTypeDisconnectHandler(client, msg); er != nil {
				_ = client.WriteError(er, msg)
			}
			break typeCondition
		case ws.TypeRequestWaiterMessageText:
			if er := waiterTypeMessageHandler(client, msg); er != nil {
				_ = client.WriteError(exception.InvalidParams.New(er.Error()), msg)
			}
			break typeCondition
		default:
			_ = client.WriteError(exception.InvalidParams.New("未知的消息类型"), msg)
			break typeCondition
		}
	}
})

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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