func ClientConnection(clientsock *websocket.Conn) { packet := pnet.NewPacket() buffer := make([]uint8, pnet.PACKET_MAXSIZE) recv, err := clientsock.Read(buffer) if err == nil { copy(packet.Buffer[0:recv], buffer[0:recv]) packet.GetHeader() g_server.ParseFirstMessage(clientsock, packet) } else { if err.Error() != "EOF" { logger.Println("Client connection error: " + err.Error()) } } }
func echoHandler(ws *websocket.Conn) { buff := make([]byte, 150) fmt.Println("connected to:", ws.Config().Location.Host) for { _, err := ws.Read(buff) if err != nil { break } fmt.Println("got:", string(buff)) _, err = ws.Write(buff) if err != nil { break } } fmt.Println("lost connection with:", ws.Config().Location.Host) availableNodes <- reverseHostMap[ws.Config().Location.Host] }
func ChatroomServer(ws *websocket.Conn) { defer ws.Close() auth := false var user *User ch := NewChatHome() // user.DoNothing() item_ws := WSConns.PushBack(ws) defer WSConns.Remove(item_ws) // SendMessage(nil, fmt.Sprintf("welcome %s join\n", "name")) r := bufio.NewReader(ws) log.Println("Connected ") for { data, err := r.ReadBytes('\n') if err != nil { SendMessage(item_ws, `{"Code":"msg","Data":"`+user.Name+` offline"}`) if b, _ := ch.IsOnline(user.Name); b { ch.RmOnlineUser(user.Name) ch.WaittingToOnline() SendMessage(item_ws, `{"Code":"OK","Data":"Welcome `+user.Name+`"}`) } else { ch.RmWaittingUser(user.Name) } SendMessage(nil, `{"Code":"online_user_count","Data": `+strconv.Itoa(ch.GetOnlineCount())+`}`) SendMessage(nil, `{"Code":"waitting_user_count","Data": `+strconv.Itoa(ch.GetWaittingCount())+`}`) // SendMessage(nil, `{"Code":"online_user_list","Data": "`+ch.OnlineListToBase64()+`"}`) // SendMessage(nil, `{"Code":"waitting_user_list","Data": "`+ch.WaittingListToBase64()+`"}`) break } log.Println("Received: " + string(data)) log.Println(`{"Code":"online_user_list","Data": "` + ch.OnlineListToBase64() + `"}`) var msg Msg err = json.Unmarshal(data, &msg) if err != nil { SendMessage(item_ws, `{"Code":"error","Data":"JSON Error","Desc":"Nothing"}`) } if msg.Code == "cookie" { for item_user := ConnUsers.Front(); item_user != nil; item_user = item_user.Next() { user, _ = item_user.Value.(*User) if user.Cookie == msg.Data { auth = true if ch.GetOnlineCount() < ch.MAX_ONLINE_COUNT && ch.GetWaittingCount() == 0 { ch.AddOnlineUser(user.Name) SendMessage(item_ws, `{"Code":"OK","Data":"Welcome `+user.Name+`"}`) } else { ch.AddWaittingUser(user.Name) SendMessage(nil, `{"Code":"wait","Data": `+strconv.Itoa(ch.GetWaittingCount())+`}`) } SendMessage(nil, `{"Code":"online_user_count","Data": `+strconv.Itoa(ch.GetOnlineCount())+`}`) SendMessage(nil, `{"Code":"waitting_user_count","Data": `+strconv.Itoa(ch.GetWaittingCount())+`}`) // SendMessage(nil, `{"Code":"online_user_list","Data": "`+ch.OnlineListToBase64()+`"}`) // SendMessage(nil, `{"Code":"waitting_user_list","Data": "`+ch.WaittingListToBase64()+`"}`) break } } } else { if !auth { SendMessage(item_ws, `{"Code":"error","Data":"Auth Error","Desc":"RELOGIN"}`) } else { // SendMessage(item_ws, `{"Code":"OK","Data":"Auth Error","Desc":"RELOGIN"}`) ControlMsg(item_ws, msg, user) } } } log.Println("Listenning Over") }