user online

This commit is contained in:
xing 2021-04-29 16:27:19 +08:00
parent 8c0fa2a217
commit b0680cb406
3 changed files with 58 additions and 65 deletions

View File

@ -8,6 +8,8 @@ import (
"fmt" "fmt"
"net" "net"
"os" "os"
"strconv"
"strings"
) )
var menu1 = make(chan int) var menu1 = make(chan int)
@ -42,7 +44,8 @@ func sendMessage(conn net.Conn) {
for { for {
fmt.Print("请输入内容: ") fmt.Print("请输入内容: ")
var i string var i string
_, err := fmt.Scanf("%s", &i) in := bufio.NewReader(os.Stdin)
_, err := fmt.Fscanf(in, "%s", &i)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
@ -50,15 +53,20 @@ func sendMessage(conn net.Conn) {
if i == "exit" { if i == "exit" {
break break
} }
a := strings.Split(i, "|")
uid := 0
if id, ok := strconv.Atoi(a[0]); ok == nil {
uid = id
}
err = process.WriteConn(conn, message.Message{ err = process.WriteConn(conn, message.Message{
Type: "user_message", Type: "user_message",
Code: 1, Code: 1,
Msg: "", Msg: "",
Data: message.UserMessage{ Data: message.UserMessage{
TotUid: 0, TotUid: uid,
FromUid: thisUser.Id, FromUid: thisUser.Id,
FromUserName: thisUser.Name, FromUserName: thisUser.Name,
Msg: i, Msg: a[1],
}, },
}) })
if err != nil { if err != nil {
@ -119,7 +127,7 @@ func addUser(conn net.Conn) {
fmt.Print("请输入姓名、密码、性别(1男2女)空格隔开:") fmt.Print("请输入姓名、密码、性别(1男2女)空格隔开:")
var name, pw string var name, pw string
var sex int8 var sex int8
_, err := fmt.Scanf("%s %s %d", &name, &pw, &sex) _, err := fmt.Fscanf(bufio.NewReader(os.Stdin), "%s %s %d", &name, &pw, &sex)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
@ -168,7 +176,7 @@ func handleMsg() { //处理
menu1 <- 1 menu1 <- 1
case "user_message": case "user_message":
m := c.Msg.Data.(*message.UserMessage) m := c.Msg.Data.(*message.UserMessage)
fmt.Printf("\r%s: %s\n", m.FromUserName, m.Msg) fmt.Printf("\r%s %s\n%s\n", m.FromUserName, m.Datetime, m.Msg)
case "online_users": case "online_users":
list := c.Msg.Data.(*message.UsersPres) list := c.Msg.Data.(*message.UsersPres)
fmt.Printf("%s\t%s\n", "id", "昵称") fmt.Printf("%s\t%s\n", "id", "昵称")
@ -179,7 +187,6 @@ func handleMsg() { //处理
case "notice": case "notice":
fmt.Printf("\n系统:%s\n", c.Msg.Msg) fmt.Printf("\n系统:%s\n", c.Msg.Msg)
} }
} }
} }
} }

View File

@ -13,6 +13,7 @@ import (
"io/ioutil" "io/ioutil"
"net" "net"
"strconv" "strconv"
"time"
) )
type Server struct { type Server struct {
@ -141,6 +142,11 @@ func (server *Server) do(s *message.LoginS, conn net.Conn) (string, error) {
} }
func (s *Server) processConn() { func (s *Server) processConn() {
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
}
}()
for { for {
select { select {
case c := <-Rchan: case c := <-Rchan:
@ -172,6 +178,44 @@ func (s *Server) processConn() {
case "user_message": case "user_message":
data := c.Msg.Data.(*message.UserMessage) data := c.Msg.Data.(*message.UserMessage)
if data.TotUid > 0 { if data.TotUid > 0 {
to := process.Get(data.TotUid)
if to == nil {
to = process.Get(data.FromUid)
if to == nil {
break
}
err := process.WriteConn(to.Conn, message.Message{
Type: "user_message",
Code: 0,
Msg: "user had offline",
Data: message.UserMessage{
FromUid: 0,
FromUserName: "系统",
TotUid: data.FromUid,
Msg: "user had offline",
Datetime: time.Now().Format("2006-01-02 15:04:05"),
},
})
if err != nil {
fmt.Println(err)
}
break
}
err := process.WriteConn(to.Conn, message.Message{
Type: "user_message",
Code: 0,
Msg: "",
Data: message.UserMessage{
FromUid: data.FromUid,
FromUserName: data.FromUserName,
TotUid: data.TotUid,
Msg: data.Msg,
Datetime: time.Now().Format("2006-01-02 15:04:05"),
},
})
if err != nil {
fmt.Println(err)
}
} else { } else {
fmt.Println(data.FromUserName, ":", data.Msg) fmt.Println(data.FromUserName, ":", data.Msg)
@ -225,65 +269,6 @@ func (server *Server) read(conn net.Conn) {
process.Read(conn, Rchan) process.Read(conn, Rchan)
/*for {
var info = make([]byte, 65536)
i, err := conn.Read(info)
if err != nil {
fmt.Println(err, "errrrrrrrrrrrrr")
return
}
msg := message.Message{}
err = json.Unmarshal(info[:i], &msg)
if err != nil {
fmt.Println(err)
}
t := message.MsgType[msg.Type]
mm := message.Message{}
mm.Data = reflect.New(t).Interface()
err = json.Unmarshal(info[:i], &mm)
if err != nil {
fmt.Println(err)
}
switch msg.Type {
case "login_send":
if d,ok:=mm.Data.(*message.LoginS);ok{
_, err := server.do(d,conn)
if err != nil {
fmt.Println(err)
return
}
}
}
/*switch reflect.TypeOf(mm.Data){
case reflect.TypeOf(&LoginS{}):
x :=mm.Data.(*LoginS)
e := s.Login(x.Uid,x.Pw)
re := Message{
Type: "login_response",
Data: nil,
}
if e != nil {
re.Data=Correspond{
Code: 0,
Msg: "login fail",
Error: "pw fail or user not exist",
}
}else{
re.Data=Correspond{
Code: 1,
Msg: "success",
Error: "",
}
}
m, _ :=json.Marshal(re)
_, err := conn.Write(m)
if err != nil {
fmt.Println(err)
}
}*/
} }
func (server *Server) Start() { func (server *Server) Start() {

View File

@ -35,6 +35,7 @@ type UserMessage struct {
FromUserName string FromUserName string
TotUid int TotUid int
Msg string Msg string
Datetime string
} }
type LoginS struct { type LoginS struct {