Exemple #1
0
func (m *multiplexer) sendClose(msgKey string) {
	m.messagesToBackend <- common.FormatMessage(msgKey, common.Close, "")
}
Exemple #2
0
func (m *multiplexer) connect(msgKey, url string) {
	m.messagesToBackend <- common.FormatMessage(msgKey, common.Connect, url)
}
Exemple #3
0
func (m *multiplexer) send(msgKey, msg string) {
	m.messagesToBackend <- common.FormatMessage(msgKey, common.Body, msg)
}
Exemple #4
0
func connectToProxyWS(ws *websocket.Conn, handlers map[string]Handler) {
	responders := make(map[string]chan string)
	responseChannel := make(chan common.Message, 10)

	// Write messages to proxy
	go func() {
		for {
			message, ok := <-responseChannel
			if !ok {
				return
			}
			data := common.FormatMessage(message.Key, message.Type, message.Body)
			ws.WriteMessage(1, []byte(data))
		}
	}()

	// Read and route messages from proxy
	for {
		_, msg, err := ws.ReadMessage()
		if err != nil {
			log.WithFields(log.Fields{"error": err}).Error("Received error reading from socket. Exiting.")
			close(responseChannel)
			return
		}

		message := common.ParseMessage(string(msg))
		switch message.Type {
		case common.Connect:
			requestUrl, err := url.Parse(message.Body)
			if err != nil {
				continue
			}

			handler, ok := getHandler(requestUrl.Path, handlers)
			if ok {
				msgChan := make(chan string, 10)
				responders[message.Key] = msgChan
				go handler.Handle(message.Key, message.Body, msgChan, responseChannel)
			} else {
				log.WithFields(log.Fields{"path": requestUrl.Path}).Warn("Could not find appropriate message handler for supplied path.")
				responseChannel <- common.Message{
					Key:  message.Key,
					Type: common.Close,
					Body: ""}
			}
		case common.Body:
			if msgChan, ok := responders[message.Key]; ok {
				msgChan <- message.Body
			} else {
				log.WithFields(log.Fields{"key": message.Key}).Warn("Could not find responder for specified key.")
				responseChannel <- common.Message{
					Key:  message.Key,
					Type: common.Close,
				}
			}
		case common.Close:
			closeHandler(responders, message.Key)
		default:
			log.WithFields(log.Fields{"messageType": message.Type}).Warn("Unrecognized message type. Closing connection.")
			closeHandler(responders, message.Key)
			SignalHandlerClosed(message.Key, responseChannel)
			continue
		}
	}
}