Skip to content

Commit

Permalink
update websocket examples - keep neffos and use the iris/websocket as…
Browse files Browse the repository at this point in the history
… helper only

Former-commit-id: c34b916e186286cd0de6d694d8bdb4f9390121a2
  • Loading branch information
kataras committed Jul 1, 2019
1 parent 69171e8 commit 35389c6
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 10,806 deletions.
13 changes: 5 additions & 8 deletions _examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,9 @@ iris session manager lives on its own [package](https://github.com/kataras/iris/

### Websockets

iris websocket library lives on its own [package](https://github.com/kataras/iris/tree/master/websocket) which depends on the [kataras/neffos](https://github.com/kataras/neffos) external package.
[WebSocket](https://wikipedia.org/wiki/WebSocket) is a protocol that enables two-way persistent communication channels over TCP connections. It is used for applications such as chat, stock tickers, games, anywhere you want real-time functionality in a web application.

Iris websocket library is now merged with the [neffos real-time framework](https://github.com/kataras/neffos) and Iris-specific helpers live on the [iris/websocket](https://github.com/kataras/iris/tree/master/websocket) subpackage. Learn neffos from its [wiki](https://github.com/kataras/neffos#learning-neffos) and have a look some of the [Iris websocket examples](websocket/).

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.

Expand All @@ -486,13 +488,8 @@ The package is designed to work with raw websockets although its API is similar
* [Go Client](websocket/basic/go-client/client.go)
* [Browser Client](websocket/basic/browser/index.html)
* [Browser NPM Client (browserify)](websocket/basic/browserify/app.js)
- [Native Messages](websocket/native-messages/main.go)
- [Connection List](websocket/connectionlist/main.go)
- [TLS Enabled](websocket/secure/main.go)
- [Custom Raw Go Client](websocket/custom-go-client/main.go)
- [Third-Party socket.io](websocket/third-party-socketio/main.go)

> You're free to use your own favourite websockets package if you'd like so.
- [Native Messages](websocket/native-messages/main.go) **UPDATED**
- [TLS Enabled](websocket/secure/README.md)

### Typescript Automation Tools

Expand Down
14 changes: 2 additions & 12 deletions _examples/README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,23 +425,13 @@ Iris session 管理独立包 [package](https://github.com/kataras/iris/tree/mast

### Websockets

iris websocket库依赖于它自己的[包](https://github.com/kataras/iris/tree/master/websocket).

设计这个包的目的是处理原始websockets,虽然它的API和著名的[socket.io](https://socket.io)很像。我最近读了一片文章,并且对我
决定给iris设计一个**快速的**websocket**限定**包并且不是一个向后传递类socket.io的包。你可以阅读这个链接里的文章https://medium.com/@ivanderbyl/why-you-don-t-need-socket-io-6848f1c871cd。

- [Basic](websocket/basic) **新**
* [Server](websocket/basic/server.go)
* [Go Client](websocket/basic/go-client/client.go)
* [Browser Client](websocket/basic/browser/index.html)
* [Browser NPM Client (browserify)](websocket/basic/browserify/app.js)
- [原生消息](websocket/native-messages/main.go)
- [连接列表](websocket/connectionlist/main.go)
- [TLS支持](websocket/secure/main.go)
- [自定义原始Go客户端](websocket/custom-go-client/main.go)
- [第三方socket.io](websocket/third-party-socketio/main.go)

> 如果你愿意,你可以自由使用你自己喜欢的websockets包。
- [原生消息](websocket/native-messages/main.go) **更新**
- [TLS支持](websocket/secure/README.md)

### Typescript 自动化工具

Expand Down
135 changes: 0 additions & 135 deletions _examples/websocket/README.md

This file was deleted.

14 changes: 8 additions & 6 deletions _examples/websocket/basic/go-client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"time"

"github.com/kataras/iris/websocket"

"github.com/kataras/neffos"
)

const (
Expand All @@ -21,17 +23,17 @@ const (
// this can be shared with the server.go's.
// `NSConn.Conn` has the `IsClient() bool` method which can be used to
// check if that's is a client or a server-side callback.
var clientEvents = websocket.Namespaces{
namespace: websocket.Events{
websocket.OnNamespaceConnected: func(c *websocket.NSConn, msg websocket.Message) error {
var clientEvents = neffos.Namespaces{
namespace: neffos.Events{
neffos.OnNamespaceConnected: func(c *neffos.NSConn, msg neffos.Message) error {
log.Printf("connected to namespace: %s", msg.Namespace)
return nil
},
websocket.OnNamespaceDisconnect: func(c *websocket.NSConn, msg websocket.Message) error {
neffos.OnNamespaceDisconnect: func(c *neffos.NSConn, msg neffos.Message) error {
log.Printf("disconnected from namespace: %s", msg.Namespace)
return nil
},
"chat": func(c *websocket.NSConn, msg websocket.Message) error {
"chat": func(c *neffos.NSConn, msg neffos.Message) error {
log.Printf("%s", string(msg.Body))
return nil
},
Expand All @@ -42,7 +44,7 @@ func main() {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(dialAndConnectTimeout))
defer cancel()

client, err := websocket.Dial(ctx, websocket.DefaultGorillaDialer, endpoint, clientEvents)
client, err := neffos.Dial(ctx, websocket.DefaultGorillaDialer, endpoint, clientEvents)
if err != nil {
panic(err)
}
Expand Down
23 changes: 13 additions & 10 deletions _examples/websocket/basic/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,29 @@ import (

"github.com/kataras/iris"
"github.com/kataras/iris/websocket"

"github.com/kataras/neffos"
)

const namespace = "default"

// if namespace is empty then simply websocket.Events{...} can be used instead.
var serverEvents = websocket.Namespaces{
namespace: websocket.Events{
websocket.OnNamespaceConnected: func(nsConn *websocket.NSConn, msg websocket.Message) error {
// if namespace is empty then simply neffos.Events{...} can be used instead.
var serverEvents = neffos.Namespaces{
namespace: neffos.Events{
neffos.OnNamespaceConnected: func(nsConn *neffos.NSConn, msg neffos.Message) error {
// with `websocket.GetContext` you can retrieve the Iris' `Context`.
ctx := websocket.GetContext(nsConn.Conn)

log.Printf("[%s] connected to namespace [%s] with IP [%s]",
nsConn, msg.Namespace,
// with `GetContext` you can retrieve the Iris' `Context`, alternatively
// you can use the `nsConn.Conn.Socket().Request()` to get the raw `*http.Request`.
websocket.GetContext(nsConn.Conn).RemoteAddr())
ctx.RemoteAddr())
return nil
},
websocket.OnNamespaceDisconnect: func(nsConn *websocket.NSConn, msg websocket.Message) error {
neffos.OnNamespaceDisconnect: func(nsConn *neffos.NSConn, msg neffos.Message) error {
log.Printf("[%s] disconnected from namespace [%s]", nsConn, msg.Namespace)
return nil
},
"chat": func(nsConn *websocket.NSConn, msg websocket.Message) error {
"chat": func(nsConn *neffos.NSConn, msg neffos.Message) error {
// room.String() returns -> NSConn.String() returns -> Conn.String() returns -> Conn.ID()
log.Printf("[%s] sent: %s", nsConn, string(msg.Body))

Expand All @@ -39,7 +42,7 @@ var serverEvents = websocket.Namespaces{

func main() {
app := iris.New()
websocketServer := websocket.New(
websocketServer := neffos.New(
websocket.DefaultGorillaUpgrader, /* DefaultGobwasUpgrader can be used too. */
serverEvents)

Expand Down
11 changes: 6 additions & 5 deletions _examples/websocket/native-messages/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"log"

"github.com/kataras/iris"

"github.com/kataras/iris/websocket"

"github.com/kataras/neffos"
)

type clientPage struct {
Expand All @@ -25,21 +26,21 @@ func main() {
// and contains only one registered event which is the `OnNativeMessage`.
// When `Events{...}` is used instead of `Namespaces{ "namespaceName": Events{...}}`
// then the namespace is empty "".
ws := websocket.New(websocket.DefaultGorillaUpgrader, websocket.Events{
websocket.OnNativeMessage: func(nsConn *websocket.NSConn, msg websocket.Message) error {
ws := neffos.New(websocket.DefaultGorillaUpgrader, neffos.Events{
neffos.OnNativeMessage: func(nsConn *neffos.NSConn, msg neffos.Message) error {
log.Printf("Server got: %s from [%s]", msg.Body, nsConn.Conn.ID())

nsConn.Conn.Server().Broadcast(nsConn, msg)
return nil
},
})

ws.OnConnect = func(c *websocket.Conn) error {
ws.OnConnect = func(c *neffos.Conn) error {
log.Printf("[%s] Connected to server!", c.ID())
return nil
}

ws.OnDisconnect = func(c *websocket.Conn) {
ws.OnDisconnect = func(c *neffos.Conn) {
log.Printf("[%s] Disconnected from server", c.ID())
}

Expand Down
6 changes: 6 additions & 0 deletions _examples/websocket/secure/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Secure Websockets

1. Run your server through `https://` (`iris.Run(iris.TLS)` or `iris.Run(iris.AutoTLS)` or a custom `iris.Listener(...)`)
2. Nothing changes inside the whole app, including the websocket side
3. The clients must dial the websocket server endpoint (i.e `/echo`) via `wss://` prefix (instead of the non-secure `ws://`), for example `wss://example.com/echo`
4. Ready to GO.
Loading

0 comments on commit 35389c6

Please sign in to comment.