-
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.
- add websocket chatroom demo #17
- Loading branch information
Showing
11 changed files
with
311 additions
and
5 deletions.
There are no files selected for viewing
Binary file not shown.
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,5 +1,9 @@ | ||
## ChartRoom | ||
|
||
## 基于tcp的聊天室 | ||
|
||
- 如果大量用户广播延迟问题 | ||
- 消息的堵塞 | ||
|
||
```md | ||
├── chatroom | ||
|
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"net" | ||
"strings" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestSendMsg(t *testing.T) { | ||
|
||
for i := 0; i < 100; i++ { | ||
|
||
go func(i int) { | ||
conn, err := net.Dial("tcp", ":2020") | ||
if err != nil { | ||
panic(err) | ||
} | ||
for j := 0; j < 1000; j++ { | ||
time.Sleep(time.Microsecond * 500) | ||
s := strings.NewReader(fmt.Sprintf("%s%d\n", "hello", i)) | ||
if _, err := io.Copy(conn, s); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
}(i) | ||
} | ||
time.Sleep(time.Second * 10) | ||
log.Println("done") | ||
} |
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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func BenchmarkBroadcaster(b *testing.B) { | ||
listener, err := net.Listen("tcp", ":0") | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
defer listener.Close() | ||
|
||
// 启动广播器 | ||
go broadcaster() | ||
|
||
// 启动客户端 | ||
var wg sync.WaitGroup | ||
for i := 0; i < b.N; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
|
||
conn, err := net.Dial("tcp", listener.Addr().String()) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
defer conn.Close() | ||
|
||
// 发送消息到服务器 | ||
fmt.Fprintf(conn, "Hello, %d\n", i) | ||
|
||
// 读取服务器发回的消息 | ||
buf := make([]byte, 1024) | ||
n, err := conn.Read(buf) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
msg := string(buf[:n]) | ||
|
||
// 检查消息是否正确 | ||
expect := fmt.Sprintf("Hello, %d\n", i) | ||
if msg != expect { | ||
b.Errorf("expect %q, but got %q", expect, msg) | ||
} | ||
conn.Close() | ||
}() | ||
} | ||
|
||
// 等待所有客户端完成测试 | ||
wg.Wait() | ||
listener.Close() | ||
} | ||
|
||
func TestBroadcasterWithBlocking(t *testing.T) { | ||
listener, err := net.Listen("tcp", ":0") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer listener.Close() | ||
|
||
// 启动广播器 | ||
go broadcaster() | ||
|
||
// 启动客户端 | ||
var wg sync.WaitGroup | ||
for i := 0; i < 10; i++ { | ||
wg.Add(1) | ||
go func(id int) { | ||
defer wg.Done() | ||
|
||
conn, err := net.Dial("tcp", listener.Addr().String()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer conn.Close() | ||
|
||
// 模拟消息延迟和阻塞 | ||
time.Sleep(5 * time.Second) | ||
|
||
// 发送消息到服务器 | ||
fmt.Fprintf(conn, "Hello, %d\n", id) | ||
|
||
// 读取服务器发回的消息 | ||
buf := make([]byte, 1024) | ||
n, err := conn.Read(buf) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
msg := string(buf[:n]) | ||
|
||
// 检查消息是否正确 | ||
expect := fmt.Sprintf("Hello, %d\n", id) | ||
if msg != expect { | ||
t.Errorf("expect %q, but got %q", expect, msg) | ||
} | ||
}(i) | ||
} | ||
|
||
// 等待所有客户端完成测试 | ||
wg.Wait() | ||
} |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
"nhooyr.io/websocket" | ||
"nhooyr.io/websocket/wsjson" | ||
) | ||
|
||
func main() { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute) | ||
defer cancel() | ||
|
||
c, _, err := websocket.Dial(ctx, "ws://localhost:2021/ws", nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer c.Close(websocket.StatusInternalError, "server eroor") | ||
|
||
err = wsjson.Write(ctx, c, "Hello WebSocket server") | ||
if err != nil { | ||
panic(err) | ||
} | ||
var v any | ||
err = wsjson.Read(ctx, c, &v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
log.Printf("recv: %v", v) | ||
c.Close(websocket.StatusNormalClosure, "") | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"nhooyr.io/websocket" | ||
"nhooyr.io/websocket/wsjson" | ||
) | ||
|
||
func main() { | ||
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { | ||
fmt.Fprintln(w, "HTTP , Hello") | ||
}) | ||
http.HandleFunc("/ws", func(w http.ResponseWriter, req *http.Request) { | ||
conn, err := websocket.Accept(w, req, nil) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
defer conn.Close(websocket.StatusInternalError, "servr error") | ||
ctx, cancel := context.WithTimeout(req.Context(), time.Second*10) | ||
defer cancel() | ||
|
||
var v any | ||
err = wsjson.Read(ctx, conn, &v) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
log.Printf("recv: %v", v) | ||
err = wsjson.Write(ctx, conn, "Hello WebSocket clinet") | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
conn.Close(websocket.StatusNormalClosure, "200") | ||
}) | ||
log.Fatal(http.ListenAndServe(":2021", nil)) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGetClientConn(t *testing.T) { | ||
main() | ||
} |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"testing" | ||
|
||
"github.com/lc-1010/OneBlogService/global" | ||
"github.com/lc-1010/OneBlogService/pkg/tracer" | ||
) | ||
|
||
func TestTacer(t *testing.T) { | ||
TSpan() | ||
} | ||
|
||
type b string | ||
|
||
func TSpan() { | ||
|
||
tracerPorvider, err := tracer.NewJaegerTrancer( | ||
"cligrpc", | ||
"127.0.0.1", | ||
"6831", | ||
) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
global.Tracer = tracerPorvider | ||
|
||
ctx := context.Background() | ||
tr := global.Tracer.Tracer("test11") | ||
pp := context.WithValue(ctx, b("ba"), b("j")) | ||
_, span := tr.Start(pp, "cli-rpc") | ||
defer span.End() | ||
fmt.Println("ok tracer") | ||
} |
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
Oops, something went wrong.