1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-11 14:05:59 +00:00

add iris websocket client side for Go and a simple chat example

Former-commit-id: af1c555b6b092a3d0484fee2e200fd8767d7239e
This commit is contained in:
Gerasimos (Makis) Maropoulos
2019-02-09 04:28:00 +02:00
parent d30f17eb3f
commit 280872fd59
9 changed files with 536 additions and 226 deletions

View File

@@ -478,6 +478,9 @@ iris websocket library lives on its own [package](https://github.com/kataras/iri
The package is designed to work with raw websockets although its API is similar to the famous [socket.io](https://socket.io). I have read an article recently and I felt very contented about my decision to design a **fast** websocket-**only** package for Iris and not a backwards socket.io-like package. You can read that article by following this link: https://medium.com/@ivanderbyl/why-you-don-t-need-socket-io-6848f1c871cd.
- [Chat](websocket/chat/main.go)
- [Chat with Iris Go Client Side](websocket/go-client) **NEW**
* [Server](websocket/go-client/server/main.go)
* [Client](websocket/go-client/client/main.go)
- [Native Messages](websocket/native-messages/main.go)
- [Connection List](websocket/connectionlist/main.go)
- [TLS Enabled](websocket/secure/main.go)

View File

@@ -431,6 +431,9 @@ iris websocket库依赖于它自己的[包](https://github.com/kataras/iris/tree
决定给iris设计一个**快速的**websocket**限定**包并且不是一个向后传递类socket.io的包。你可以阅读这个链接里的文章https://medium.com/@ivanderbyl/why-you-don-t-need-socket-io-6848f1c871cd。
- [聊天](websocket/chat/main.go)
- [Chat with Iris Go Client Side](websocket/go-client) **NEW**
* [Server](websocket/go-client/server/main.go)
* [Client](websocket/go-client/client/main.go)
- [原生消息](websocket/native-messages/main.go)
- [连接列表](websocket/connectionlist/main.go)
- [TLS支持](websocket/secure/main.go)

View File

@@ -87,7 +87,7 @@ func SendMessage(serverID, to, method, message string) error {
// SendtBytes broadcast a message to server
func SendtBytes(serverID, to, method string, message []byte) error {
// look https://github.com/kataras/iris/blob/master/websocket/message.go , client.go and client.js
// look https://github.com/kataras/iris/blob/master/websocket/message.go , client.js.go and client.js
// to understand the buffer line:
buffer := []byte(fmt.Sprintf("%s%v;0;%v;%v;", websocket.DefaultEvtMessageKey, method, serverID, to))
buffer = append(buffer, message...)

View File

@@ -0,0 +1,58 @@
package main
import (
"bufio"
"fmt"
"os"
"github.com/kataras/iris/websocket"
)
const (
url = "ws://localhost:8080/socket"
prompt = ">> "
)
/*
How to run:
Start the server, if it is not already started by executing `go run ../server/main.go`
And open two or more terminal windows and start the clients:
$ go run main.go
>> hi!
*/
func main() {
conn, err := websocket.Dial(url, websocket.DefaultEvtMessageKey)
if err != nil {
panic(err)
}
conn.OnError(func(err error) {
fmt.Printf("error: %v", err)
})
conn.OnDisconnect(func() {
fmt.Println("Server was force-closed[see ../server/main.go#L19] this connection after 20 seconds, therefore I am disconnected.")
os.Exit(0)
})
conn.On("chat", func(message string) {
fmt.Printf("\n%s\n", message)
})
fmt.Println("Start by typing a message to send")
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print(prompt)
if !scanner.Scan() || scanner.Err() != nil {
break
}
msgToSend := scanner.Text()
if msgToSend == "exit" {
break
}
conn.Emit("chat", msgToSend)
}
fmt.Println("Terminated.")
}

View File

@@ -0,0 +1,32 @@
package main
import (
"fmt"
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/websocket"
)
func main() {
app := iris.New()
ws := websocket.New(websocket.Config{})
app.Get("/socket", ws.Handler())
ws.OnConnection(func(c websocket.Connection) {
go func() {
<-time.After(20 * time.Second)
c.Disconnect()
}()
c.On("chat", func(message string) {
c.To(websocket.Broadcast).Emit("chat", c.ID()+": "+message)
})
c.OnDisconnect(func() {
fmt.Printf("Connection with ID: %s has been disconnected!\n", c.ID())
})
})
app.Run(iris.Addr(":8080"))
}