Ejemplo n.º 1
0
func handleWebSocket(conn *websocket.Conn) {
	respStatus := http.StatusOK
	defer func() {
		conn.Close()
		logRequest(HTTPS_WEBSOCKET, respStatus, conn.Request().Host, conn.Request())
	}()
	request := make([]byte, 1024)
	for {
		n, err := conn.Read(request)
		if err != nil {
			if debugMode {
				log.Error("Error reading on WebSocket: %s", err)
			}
			break
		}
		response, status := getLiveItems(string(request[:n]))
		if status != http.StatusOK {
			respStatus = status
			break
		}
		if len(response) != 0 {
			if _, err = conn.Write(response); err != nil {
				break
			}
		}
	}
}
Ejemplo n.º 2
0
func (m *model) subscribe(s *websocket.Conn) {
	data, err := json.MarshalIndent(newConnectMessage(m.Changes, m.Kittens, m.VersionIdentifier), "", "  ")
	if err != nil {
		s.Close()
		return
	}

	_, err = s.Write(data)
	if err != nil {
		s.Close()
		return
	}

	m.Conns[s] = 1
}
Ejemplo n.º 3
0
func handler(ws *websocket.Conn) {
	x := 0.
	for {
		if x >= 2*math.Pi {
			x = 0
		} else {
			x += 0.05
		}

		time.Sleep(500 * 1000 * 1000) // sleep for 500ms (Sleep takes nanoseconds)

		msg := strconv.Ftoa64(math.Sin(x), 'g', 10)
		log.Printf("%v sending: %v\n", ws, msg)
		ws.Write([]byte(msg))
	}
}
Ejemplo n.º 4
0
func send(ws *websocket.Conn, path string, evs <-chan store.Event) {
	l := len(path) - 1
	for ev := range evs {
		ev.Getter = nil // don't marshal the entire snapshot
		ev.Path = ev.Path[l:]
		b, err := json.Marshal(ev)
		if err != nil {
			log.Println(err)
			return
		}
		_, err = ws.Write(b)
		if err != nil {
			log.Println(err)
			return
		}
	}
}
Ejemplo n.º 5
0
func WebSocketServer(ws *websocket.Conn) {
	fmt.Println("WebSocketServer")
	var msg [1024]byte
	ch := CreateReadChannel(ws, msg[:])
	interval := time.Tick(5 * 1e9)
Loop:
	for {
		select {
		case n := <-ch:
			if n == 0 {
				fmt.Println("The connection was closed by peer.")
				break Loop
			}
			ws.Write(msg[:n])
		case _ = <-interval:
			ws.Write([]byte("An interval event fired in a server side."))
		}
	}
}
Ejemplo n.º 6
0
func SocketServer(ws *websocket.Conn) {
	c := make(chan []byte, 100)
	e := ws_channels.PushBack(c)
	fmt.Printf("New connection:    %v total\n", ws_channels.Len())
	var data []byte
	for {
		select {
		case data = <-c:
		case <-time.After(5e9): // make sure we're still connected
			data = []byte("")
		}

		if _, err := ws.Write(data); err != nil {
			// fmt.Println("Closing")
			ws.Close()
			break
		}
	}
	ws_channels.Remove(e)
	fmt.Printf("Closed connection: %v total\n", ws_channels.Len())
}
Ejemplo n.º 7
0
func doEventStream(ws *websocket.Conn) {
	ID := chat.UUID()
	defer func() {
		subscriptionChan <- &Subscription{ws, chat.REMOVE_CONNECTION, &User{ID, nil, ""}}
		ws.Close()
	}()

	subscriptionChan <- &Subscription{ws, chat.ADD_CONNECTION, &User{ID, nil, ""}}
	newStartMessage, _ := json.Marshal(
		MessageData{
			"start",
			map[string]interface{}{
				"ID": ID,
			}})

	ws.Write(newStartMessage)

	for {
		buf := make([]byte, 1024)

		n, err := ws.Read(buf)
		if err != nil {
			log.Println("Error reading from websocket connection: ", err.Error())
			break
		}

		newMessageData := new(MessageData)
		err = json.Unmarshal(buf[0:n], newMessageData)

		if err != nil {
			log.Println("Error unmarshaling message: ", string(buf[0:n]), " : ", err.Error())
		}

		messageChan <- message{
			ws,
			*newMessageData,
		}
	}
}
Ejemplo n.º 8
0
func wssServe(ws *websocket.Conn) {
	fmt.Println("Socket connect")
	ws.Write([]byte(strconv.Itoa(int(*state))))
	b := make([]byte, 2)
	var quit bool
	for !quit {
		ws.Read(b)
		t, _ := strconv.Atoi(string(b))
		fmt.Println(t)
		switch t {
		case 0:
			func() {
				fmt.Println("Server Init")
				if *state > 0 {
					ws.Write([]byte(strconv.Itoa(int(*state))))
				} else {
					ws.Write([]byte("0"))
					b = make([]byte, 5)
					ws.Read(b)
					world, _ := strconv.Atob(string(b))
					fmt.Println(world)
					if world {

					} else {

					}
				}
			}()
		default:
			func() {
				fmt.Println(string(b))
				quit = true
			}()
		}
		quit = true
	}
}
Ejemplo n.º 9
0
/*
 * Process image
 */
func process(input *ProcessInput, conn *websocket.Conn, stopCh chan bool) os.Error {
	log.Println(input)

	// open input file
	inputFile, err := os.OpenFile(UploadDir+input.Image, os.O_RDONLY, 0666)
	if err != nil {
		return err
	}
	defer inputFile.Close()

	// create output file
	outputFile, err := os.OpenFile(ResultDir+input.Image, os.O_CREATE|os.O_WRONLY, 0666)
	if err != nil {
		return err
	}
	defer outputFile.Close()

	// decode png image
	inputImage, _, err := image.Decode(inputFile)
	if err != nil {
		return err
	}
	rgbaInput := rgba(inputImage)

	sivqParams := SIVQParameters{
		GammaAdjustment: float(input.GammaAdjust),
		AverageBias:     float(input.AverageBias),
		RotationStride:  float(input.RotationStride),
		MatchingStride:  input.MatchStride,
		MatchingOffset:  input.MatchingOffset,
		Threshold:       float(input.Threshold),
		ProgressCallback: func(p float) {
			conn.Write([]byte(strconv.Ftoa32(float32(p), 'f', 4)))
		},
		StopCh: stopCh}

	// get vector
	var ringVector *RingVector
	if len(input.VectorName) == 0 {
		vectorParams := RingVectorParameters{
			Radius:    input.VectorRadius,
			Count:     input.VectorRings,
			RadiusInc: input.RingSizeInc}

		ringVector = NewRingVector(vectorParams)
		ringVector.LoadData(rgbaInput, input.VecX, input.VecY)
	} else {
		// load vector from file
		vectorFile, err := os.OpenFile(VectorDir+input.VectorName, os.O_RDONLY, 0666)
		if err != nil {
			return err
		}
		defer vectorFile.Close()

		decoder := gob.NewDecoder(vectorFile)
		err = decoder.Decode(&ringVector)
		if err != nil {
			return err
		}
	}

	// do the magic
	outputImage := SIVQ(sivqParams, rgbaInput, ringVector)

	if err = png.Encode(outputFile, outputImage); err != nil {
		return err
	}
	return nil
}