示例#1
0
func StartListeningForWebSocket(port string, collection *mgo.Collection) {
	http.Handle("/", websocket.Handler(func(ws *websocket.Conn) {
		//var in []byte
		var in string

		err := websocket.Message.Receive(ws, &in)
		fault.Handle(err)

		fmt.Println(in)

		handleMessage(in, collection)
	}))

	err := http.ListenAndServe(port, nil)
	fault.Handle(err)
}
示例#2
0
func HandleLiveConnection(conn net.Conn, wg sync.WaitGroup, collection *mgo.Collection) {
	defer wg.Done()
	fmt.Println("Connection accepted!")

	for {
		msg, err := bufio.NewReader(conn).ReadString('\n')
		fault.Handle(err)

		fmt.Println(msg)

		handleMessage(msg, collection)

		conn.Write([]byte("Done!"))
	}
}
示例#3
0
func handleMessage(message string, collection *mgo.Collection) {
	sentences := data.GetSentences(message)

	var sentencePairs [][]data.WordPair

	for _, sentence := range sentences {
		sentencePairs = append(sentencePairs, data.GetPairs(sentence))
	}

	for _, sentencePair := range sentencePairs {
		for _, pair := range sentencePair {
			_, err := collection.Upsert(bson.M{pair.Head: pair.Tail}, bson.M{"$inc": bson.M{"weight": 0.1}})
			fault.Handle(err)
		}
	}
}
示例#4
0
func GetConnection(host string) *mgo.Session {
	session, err := mgo.Dial(host)
	fault.Handle(err)

	return session
}
示例#5
0
func Accept(ln net.Listener) net.Conn {
	conn, err := ln.Accept()
	fault.Handle(err)

	return conn
}
示例#6
0
func StartListening(port string) net.Listener {
	ln, err := net.Listen("tcp", port)
	fault.Handle(err)

	return ln
}