-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Improve chatroom functionality
- Enable user input capture and message sending using `bufio.NewScanner(os.Stdin)` - Enhance broadcaster - Add `getNew` function returning emojis name - Implement WebSocket version similar to TCP #17
- Loading branch information
Showing
5 changed files
with
145 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,143 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"net" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/gobwas/ws" | ||
"github.com/gobwas/ws/wsutil" | ||
emoji "github.com/tmdvs/Go-Emoji-Utils" | ||
) | ||
|
||
func main() { | ||
userIdListChannel <- 0 | ||
go broadcaster() | ||
http.HandleFunc("/ws", handleConn) | ||
http.ListenAndServe(":8080", nil) | ||
} | ||
|
||
http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
conn, _, _, err := ws.UpgradeHTTP(r, w) | ||
if err != nil { | ||
panic(err) | ||
} | ||
go func() { | ||
defer conn.Close() | ||
for { | ||
msg, op, err := wsutil.ReadClientData(conn) | ||
if err != nil { | ||
log.Printf("read:%v\n", err) | ||
break | ||
} | ||
log.Println(msg) | ||
func getID() int { | ||
id := <-userIdListChannel | ||
userIdListChannel <- id + 1 | ||
return id + 1 | ||
} | ||
|
||
func handleConn(w http.ResponseWriter, r *http.Request) { | ||
conn, _, _, err := ws.UpgradeHTTP(r, w) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = wsutil.WriteServerMessage(conn, op, []byte("Hello server")) | ||
if err != nil { | ||
log.Printf("write:%v\n", err) | ||
break | ||
user := User{ | ||
ID: getID(), | ||
Name: getName(r.RemoteAddr), | ||
conn: &conn, | ||
} | ||
enteringChannel <- user | ||
wsutil.WriteServerMessage(conn, ws.OpText, | ||
[]byte(fmt.Sprintf("%s,%s!", "hello", user.Name))) | ||
|
||
go func(leavingChannel chan User) { | ||
defer func() { | ||
leavingChannel <- user | ||
conn.Close() | ||
}() | ||
for { | ||
msg, _, err := wsutil.ReadClientData(conn) | ||
if err != nil { | ||
if err != io.EOF { | ||
log.Println(err) | ||
} | ||
break | ||
} | ||
}() | ||
})) | ||
log.Println(string(msg)) | ||
newMsg := fmt.Sprintf("[%d-%s]:%s", user.ID, user.Name, string(msg)) | ||
messageChannel <- []byte(newMsg) | ||
} | ||
|
||
}(leavingChannel) | ||
} | ||
|
||
var ( | ||
messageChannel = make(chan string, 8) | ||
messageChannel = make(chan []byte, 8) | ||
userIdListChannel = make(chan int, 1) | ||
closeChannel = make(chan struct{}) | ||
enteringChannel = make(chan User) | ||
leavingChannel = make(chan User) | ||
//closeChannel = make(chan struct{}) | ||
enteringChannel = make(chan User) | ||
leavingChannel = make(chan User) | ||
) | ||
|
||
type User struct { | ||
ID int | ||
Name string | ||
Addr string | ||
Msg chan string | ||
conn *net.Conn | ||
} | ||
|
||
func broadcaster() { | ||
usersMap := make(map[*User]struct{}) | ||
for { | ||
select { | ||
case user := <-enteringChannel: | ||
userIdListChannel <- user.ID | ||
case user := <-leavingChannel: | ||
delete(usersMap, &user) | ||
//userIdListChannel <- user.ID 没有一值处理结果满了阻塞了 | ||
usersMap[&user] = struct{}{} | ||
log.Println(user.ID, "enter", len(usersMap)) | ||
case lu := <-leavingChannel: | ||
delete(usersMap, &lu) | ||
log.Println(lu.ID, "leave") | ||
case msg := <-messageChannel: | ||
log.Println("got messge", len(usersMap)) | ||
for user := range usersMap { | ||
if strings.Split(strings.Trim(string(msg), "["), "-")[0] != strconv.Itoa(user.ID) { | ||
err := wsutil.WriteServerMessage(*user.conn, ws.OpText, []byte(msg)) | ||
if err != nil { | ||
log.Printf("write:%v\n", err) | ||
delete(usersMap, user) | ||
continue | ||
} | ||
} | ||
} | ||
log.Println("send done") | ||
} | ||
|
||
} | ||
} | ||
|
||
func getName(s string) string { | ||
|
||
// 1. 提取数字 | ||
numStr := strings.Split(s, ":")[3] | ||
num, _ := strconv.ParseInt(numStr, 10, 64) | ||
fmt.Println(numStr, num, s) | ||
// 2. 转16进制 | ||
hex := fmt.Sprintf("%X", num) | ||
|
||
// 3. 转整数 | ||
val, _ := strconv.ParseInt(hex, 16, 32) | ||
// 4. 检查范围 | ||
// 5. 映射到范围内 | ||
valTmp := val>>10 + 0x1F600 | ||
fmt.Println("tmp", valTmp) | ||
// 6. 检查返回的emoji是否合法 | ||
for i := 0; i < 5; i++ { | ||
e, err := emoji.LookupEmoji(string(rune(val))) | ||
fmt.Printf("ok-->%#v %v\n", e, err) | ||
if err == nil { | ||
return e.Value | ||
} else { | ||
val = valTmp | ||
} | ||
|
||
val++ | ||
|
||
} | ||
fmt.Println(val, "ooooo") | ||
// 8. 还是无法获取,返回默认emoji | ||
return string(rune(0x1F601)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters