Ejemplo n.º 1
0
func setupChats() map[string]hub.ChatHub {
	d_hubs := map[string]hub.ChatHub{}
	c, _ := hub.GetRedis()
	defer c.Close()
	s, _ := redis.Strings(c.Do("KEYS", "*"))
	var documents = map[string]document.Document{}
	for _, str := range s {
		jdoc, err := redis.String(c.Do("GET", str))
		var doc document.Document
		error := json.Unmarshal([]byte(jdoc), &doc)
		if error != nil {
			fmt.Println("chat setup error:", err)
		}
		documents[doc.Name] = doc
	}
	for _, doc := range documents {
		var h = hub.ChatHub{
			Document:    doc,
			Broadcast:   make(chan []byte),
			Register:    make(chan *hub.ChatConnection),
			Unregister:  make(chan *hub.ChatConnection),
			Connections: make(map[*hub.ChatConnection]bool),
		}
		go h.Run()
		d_hubs[doc.Name] = h
	}
	return d_hubs
}
Ejemplo n.º 2
0
// documentStream handles websocket requests from the client for a particular document
func documentStream(ctx *web.Context, documentId string) {
	w := ctx.ResponseWriter
	r := ctx.Request
	if r.Header.Get("Origin") != "http://"+r.Host {
		ctx.Abort(403, "Origin not allowed")
		// http.Error(w, "Origin not allowed", 403)
		return
	}
	ws, err := websocket.Upgrade(w, r.Header, nil, 1024, 1024)
	if _, ok := err.(websocket.HandshakeError); ok {
		ctx.Abort(400, "Not a websocket handshake")
		return
	} else if err != nil {
		log.Println(err)
		return
	}
	redis_conn, _ := hub.GetRedis()
	defer redis_conn.Close()
	s, err := redis.String(redis_conn.Do("GET", documentId))
	//make new document at that stringId
	if err != nil {
		doc := document.NewDoc(documentId)
		document_hubs[doc.Name] = hub.DocumentHub{
			Document:    doc,
			Broadcast:   make(chan hub.Message),
			Register:    make(chan *hub.DocumentConnection),
			Unregister:  make(chan *hub.DocumentConnection),
			Connections: make(map[*hub.DocumentConnection]bool),
		}
		h := document_hubs[doc.Name]
		go h.Run()
		json_bytes, _ := json.Marshal(doc)
		redis_conn.Do("SET", documentId, string(json_bytes))
		c := &hub.DocumentConnection{Send: make(chan hub.Message, 256), Ws: ws, H: document_hubs[doc.Name]}
		document_hubs[doc.Name].Register <- c
		mainHub.Broadcast <- json_bytes
		go c.WritePump()
		c.ReadPump()
	} else {
		var doc document.Document
		fmt.Println(s)
		err := json.Unmarshal([]byte(s), &doc)
		if err != nil {
			fmt.Println("Error:", err, s)
		}
		c := &hub.DocumentConnection{Send: make(chan hub.Message, 256), Ws: ws, H: document_hubs[doc.Name]}
		document_hubs[doc.Name].Register <- c
		go c.WritePump()
		c.ReadPump()
	}
}
Ejemplo n.º 3
0
func deleteDocument(ctx *web.Context, documentId string) {
	redis_conn, _ := hub.GetRedis()
	defer redis_conn.Close()

	//make new document at that stringId
	if _, ok := document_hubs[documentId]; ok {
		h := document_hubs[documentId]
		var buf bytes.Buffer
		doc := document.Document{Name: h.Document.Name, Title: h.Document.Title, Version: h.Document.Version}
		redis_conn.Do("DEL", documentId)
		json_bytes, _ := json.Marshal(doc)
		h.Broadcast <- hub.Message{M: json_bytes}
		buf.Write(json_bytes)
		io.Copy(ctx, &buf)
	} else {
		ctx.Abort(404, "Document does not exist")
		return
	}
}
Ejemplo n.º 4
0
func getDocuments(ctx *web.Context) {
	c, _ := hub.GetRedis()
	defer c.Close()
	s, _ := redis.Strings(c.Do("KEYS", "*"))
	fmt.Printf("%#v\n", s)
	var documents = map[string]document.Document{}
	for _, str := range s {
		jdoc, err := redis.String(c.Do("GET", str))
		var doc document.Document
		error := json.Unmarshal([]byte(jdoc), &doc)
		if error != nil {
			fmt.Println("Document setup error:", err)
		}
		documents[doc.Name] = doc
		fmt.Println("Document: ", doc.Name)
	}

	var buf bytes.Buffer
	json_bytes, _ := json.Marshal(documents)
	buf.Write(json_bytes)
	io.Copy(ctx, &buf)
}
Ejemplo n.º 5
0
func setDocumentTitle(ctx *web.Context, documentId string) {
	redis_conn, _ := hub.GetRedis()
	defer redis_conn.Close()

	//make new document at that stringId
	if _, ok := document_hubs[documentId]; ok {
		body, _ := ioutil.ReadAll(ctx.Request.Body)
		var ndoc document.Document
		json.Unmarshal(body, &ndoc)
		h := document_hubs[documentId]
		h.Document.Title = ndoc.Title
		var buf bytes.Buffer
		doc := document.Document{Name: h.Document.Name, Title: h.Document.Title, Version: h.Document.Version}
		h.Save(redis_conn)
		doc.ClientId = ndoc.ClientId
		json_bytes, _ := json.Marshal(doc)
		mainHub.Broadcast <- json_bytes
		buf.Write(json_bytes)
		io.Copy(ctx, &buf)
	} else {
		ctx.Abort(404, "Document does not exist")
		return
	}
}