-
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.
- Loading branch information
Showing
8 changed files
with
343 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
) | ||
|
||
func pack2uhex(size int, data interface{}) []byte { | ||
switch size { | ||
case 1: | ||
return []byte{uint8(data.(uint64))} | ||
case 2: | ||
buf := make([]byte, 2) | ||
binary.BigEndian.PutUint16(buf, uint16(data.(uint64))) | ||
return buf | ||
case 3: | ||
buf := make([]byte, 3) | ||
values := data.([]uint64) | ||
buf[0] = uint8(values[0] >> 8) | ||
buf[1] = uint8(values[0]) | ||
buf[2] = uint8(values[1]) | ||
return buf | ||
case 4: | ||
buf := make([]byte, 4) | ||
binary.BigEndian.PutUint32(buf, uint32(data.(uint64))) | ||
return buf | ||
case 5: | ||
buf := make([]byte, 5) | ||
values := data.([]uint64) | ||
binary.BigEndian.PutUint32(buf, uint32(values[0])) | ||
buf[4] = uint8(values[1]) | ||
return buf | ||
case 6: | ||
buf := make([]byte, 6) | ||
values := data.([]uint64) | ||
binary.BigEndian.PutUint16(buf[2:], uint16(values[0])) | ||
buf[5] = byte(values[1]) | ||
return buf | ||
case 7: | ||
buf := make([]byte, 7) | ||
values := data.([]uint64) | ||
binary.BigEndian.PutUint32(buf, uint32(values[0])) | ||
buf[5] = byte(values[1]) | ||
buf[6] = byte(values[2]) | ||
return buf | ||
case 8: | ||
buf := make([]byte, 8) | ||
binary.BigEndian.PutUint64(buf, data.(uint64)) | ||
return buf | ||
case 9: | ||
buf := make([]byte, 9) | ||
values := data.([]uint64) | ||
binary.BigEndian.PutUint64(buf, values[0]) | ||
buf[8] = uint8(values[1]) | ||
return buf | ||
case 10: | ||
buf := make([]byte, 10) | ||
values := data.([]uint64) | ||
binary.BigEndian.PutUint64(buf, values[0]) | ||
binary.BigEndian.PutUint16(buf[8:], uint16(values[1])) | ||
return buf | ||
case 11: | ||
buf := make([]byte, 11) | ||
values := data.([]uint64) | ||
binary.BigEndian.PutUint64(buf, values[0]) | ||
binary.BigEndian.PutUint16(buf[8:], uint16(values[1])) | ||
buf[10] = uint8(values[2]) | ||
return buf | ||
default: | ||
panic("please pack it yourself") | ||
} | ||
} | ||
|
||
func main() { | ||
//data1 := uint64(42) | ||
//data2 := uint64(1024) | ||
//data3 := []uint64{255, 42} | ||
//data4 := uint64(123456789) | ||
//data5 := []uint64{123456789, 255} | ||
//data6 := []uint64{65536, 42} | ||
//data7 := []uint64{65536, 42, 255} | ||
//data8 := uint64(12345678901234567890) | ||
//data9 := []uint64{12345678901234567890, 255} | ||
//data10 := []uint64{12345678901234567890, 65535} | ||
//data11 := []uint64{12345678901234567890, 65535, 255} | ||
|
||
data1 := uint64(99) | ||
data2 := uint64(2048) | ||
data3 := []uint64{128, 99} | ||
data4 := uint64(987654321) | ||
data5 := []uint64{987654321, 128} | ||
data6 := []uint64{8192, 99} | ||
data7 := []uint64{8192, 99, 128} | ||
data8 := uint64(9876543210123456789) | ||
data9 := []uint64{9876543210123456789, 128} | ||
data10 := []uint64{9876543210123456789, 32767} | ||
data11 := []uint64{9876543210123456789, 32767, 128} | ||
|
||
testData := map[int]interface{}{ | ||
1: data1, | ||
2: data2, | ||
3: data3, | ||
4: data4, | ||
5: data5, | ||
6: data6, | ||
7: data7, | ||
8: data8, | ||
9: data9, | ||
10: data10, | ||
11: data11, | ||
} | ||
|
||
for size := 1; size <= 11; size++ { | ||
data := testData[size] | ||
result := pack2uhex(size, data) | ||
fmt.Printf("size=%d, data=%v, result=%v\n", size, data, result) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// client.go | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"os/signal" | ||
"time" | ||
|
||
"github.com/gorilla/websocket" | ||
) | ||
|
||
var done chan interface{} | ||
var interrupt chan os.Signal | ||
|
||
func receiveHandler(connection *websocket.Conn) { | ||
defer close(done) | ||
for { | ||
_, msg, err := connection.ReadMessage() | ||
if err != nil { | ||
log.Println("Error in receive:", err) | ||
return | ||
} | ||
log.Printf("Received: %s\n", msg) | ||
} | ||
} | ||
|
||
func main() { | ||
done = make(chan interface{}) // Channel to indicate that the receiverHandler is done | ||
interrupt = make(chan os.Signal) // Channel to listen for interrupt signal to terminate gracefully | ||
|
||
signal.Notify(interrupt, os.Interrupt) // Notify the interrupt channel for SIGINT | ||
|
||
socketUrl := "ws://localhost:8080" + "/socket" | ||
conn, _, err := websocket.DefaultDialer.Dial(socketUrl, nil) | ||
if err != nil { | ||
log.Fatal("Error connecting to Websocket Server:", err) | ||
} | ||
defer conn.Close() | ||
go receiveHandler(conn) | ||
|
||
// Our main loop for the client | ||
// We send our relevant packets here | ||
for { | ||
select { | ||
case <-time.After(time.Duration(1) * time.Millisecond * 1000): | ||
// Send an echo packet every second | ||
err := conn.WriteMessage(websocket.TextMessage, []byte("Hello from GolangDocs!")) | ||
if err != nil { | ||
log.Println("Error during writing to websocket:", err) | ||
return | ||
} | ||
|
||
case <-interrupt: | ||
// We received a SIGINT (Ctrl + C). Terminate gracefully... | ||
log.Println("Received SIGINT interrupt signal. Closing all pending connections") | ||
|
||
// Close our websocket connection | ||
err := conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) | ||
if err != nil { | ||
log.Println("Error during closing websocket:", err) | ||
return | ||
} | ||
|
||
select { | ||
case <-done: | ||
log.Println("Receiver Channel Closed! Exiting....") | ||
case <-time.After(time.Duration(1) * time.Second): | ||
log.Println("Timeout in closing receiving channel. Exiting....") | ||
} | ||
return | ||
} | ||
} | ||
} |
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 ( | ||
"fmt" | ||
"github.com/gorilla/websocket" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
var upgrader = websocket.Upgrader{} // use default options | ||
|
||
func socketHandler(w http.ResponseWriter, r *http.Request) { | ||
conn, err := upgrader.Upgrade(w, r, nil) | ||
if err != nil { | ||
log.Fatalf("Failed to set websocket upgrade: %+v", err) | ||
return | ||
} | ||
defer conn.Close() | ||
|
||
for { | ||
messageType, message, err := conn.ReadMessage() | ||
if err != nil { | ||
log.Println("Errro during message reading:", err) | ||
break | ||
} | ||
log.Printf("Received: %s", message) | ||
err = conn.WriteMessage(messageType, append(message, []byte(":这是回复")...)) | ||
if err != nil { | ||
log.Println("Error during message writing:", err) | ||
break | ||
} | ||
} | ||
|
||
} | ||
|
||
func home(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintf(w, "Index Page") | ||
} | ||
|
||
func main() { | ||
http.HandleFunc("/socket", socketHandler) | ||
http.HandleFunc("/", home) | ||
log.Fatal(http.ListenAndServe(":8080", 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,43 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
func solve() { | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
for i := 0; i <= 15; i++ { | ||
select { | ||
case <-ctx.Done(): | ||
println("timeout") | ||
return | ||
default: | ||
time.Sleep(1 * time.Second) | ||
println(i) | ||
} | ||
} | ||
println("done") | ||
} | ||
|
||
func solve1() { | ||
ch := make(chan struct{}) | ||
go func() { | ||
for i := 0; i < 5; i++ { | ||
time.Sleep(1 * time.Second) | ||
println(i) | ||
} | ||
ch <- struct{}{} | ||
}() | ||
select { | ||
case <-ch: | ||
println("正常结束") | ||
case <-time.After(10 * time.Second): | ||
println("超时了") | ||
} | ||
} | ||
func main() { | ||
//solve() | ||
solve1() | ||
} |
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,40 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"fmt" | ||
"log" | ||
) | ||
|
||
func main() { | ||
var num int64 = 15 // 64位整数,8个字节 | ||
var buf bytes.Buffer | ||
err := binary.Write(&buf, binary.BigEndian, num) | ||
if err != nil { | ||
log.Fatal() | ||
} | ||
|
||
bytes := buf.Bytes() | ||
fmt.Println(bytes) // [0 0 0 0 0 0 0 15] | ||
var decodingNum int64 | ||
err = binary.Read(&buf, binary.BigEndian, &decodingNum) | ||
if err != nil { | ||
log.Fatal() | ||
} | ||
fmt.Println(decodingNum) // 15 | ||
|
||
buf.Reset() | ||
err = binary.Write(&buf, binary.LittleEndian, num) | ||
if err != nil { | ||
log.Fatal() | ||
} | ||
bytes = buf.Bytes() | ||
fmt.Println(bytes) // [15 0 0 0 0 0 0 0] | ||
err = binary.Read(&buf, binary.LittleEndian, &decodingNum) | ||
if err != nil { | ||
log.Fatal() | ||
} | ||
fmt.Println(decodingNum) // 15 | ||
|
||
} |
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,21 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
) | ||
|
||
//bytes.Buffer 进行多个bytes得连接 | ||
|
||
func main() { | ||
b1 := []byte("this is a first string") | ||
|
||
b2 := []byte("this is a second string") | ||
var buffer bytes.Buffer | ||
buffer.Write(b1) | ||
buffer.Write(b2) | ||
b3 := buffer.Bytes() | ||
fmt.Println(b1) | ||
fmt.Println(b2) | ||
fmt.Println(string(b3)) | ||
} |