Skip to content

Commit

Permalink
v4.0.6:
Browse files Browse the repository at this point in the history
Supported lib net/http
  • Loading branch information
doquangtan committed Jan 12, 2025
1 parent c304fda commit 8e8fdfa
Show file tree
Hide file tree
Showing 8 changed files with 490 additions and 106 deletions.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,35 @@ import "github.com/doquangtan/socket.io/v4"
and use `socketio` as the package name inside the code.

# Documents

## Server

### Constructor

#### socketio.New

Using with standard library [net/http](https://pkg.go.dev/net/http)

```go
import (
"net/http"
socketio "github.com/doquangtan/socket.io/v4"
)

func main() {
io := socketio.New()

io.OnConnection(func(socket *socketio.Socket) {
// ...
})

http.Handle("/socket.io/", io.HttpHandler())
http.ListenAndServe(":3000", nil)
}
```

Using with fiber framework [Go Fiber](https://gofiber.io)

```go
import (
socketio "github.com/doquangtan/socket.io/v4"
Expand All @@ -54,23 +79,29 @@ func main() {
```

### Events

#### Event: 'connection'

```go
io.OnConnection(func(socket *socketio.Socket) {
// ...
})
```

### Methods

#### server.emit(eventName[, ...args])

```go
io.Emit("hello")
```

```go
io.Emit("hello", 1, "2", map[string]interface{}{"3": 4})
```

#### server.of(nsp)

```go
adminNamespace := io.Of("/admin")

Expand All @@ -80,19 +111,25 @@ adminNamespace.OnConnection(func(socket *socketio.Socket) {
```

#### server.to(room)

```go
io.To("room-101").Emit("hello", "world")
```

#### server.fetchSockets()

```go
sockets := io.Sockets()
```

## Namespace

### Events

#### Event: 'connection'

Fired upon a connection from client.

```go
// main namespace
io.OnConnection(func(socket *socketio.Socket) {
Expand All @@ -106,15 +143,19 @@ io.Of("/admin").OnConnection(func(socket *socketio.Socket) {
```

### Methods

#### namespace.emit(eventName[, ...args])

```go
io.Of("/admin").Emit("hello")
```

```go
io.Of("/admin").Emit("hello", 1, "2", map[string]interface{}{"3": 4})
```

#### namespace.to(room)

```go
adminNamespace := io.Of("/admin")

Expand All @@ -124,16 +165,21 @@ adminNamespace.To("room-101").To("room-102").Emit("hello", "world")
```

#### namespace.fetchSockets()

Returns the matching Socket instances:

```go
adminNamespace := io.Of("/admin")

sockets := adminNamespace.Sockets()
```

## Socket

### Events

#### Event: 'disconnect'

```go
io.OnConnection(func(socket *socketio.Socket) {
socket.On("disconnect", func(event *socketio.EventPayload) {
Expand All @@ -143,14 +189,19 @@ io.OnConnection(func(socket *socketio.Socket) {
```

### Methods

#### socket.on(eventName, callback)

Register a new handler for the given event.

```go
socket.On("news", func(event *socketio.EventPayload) {
print(event.Data)
})
```

with several arguments

```go
socket.On("news", func(event *socketio.EventPayload) {
if len(event.Data) > 0 && event.Data[0] != nil {
Expand All @@ -164,7 +215,9 @@ socket.On("news", func(event *socketio.EventPayload) {
}
})
```

or with acknowledgement

```go
socket.On("news", func(event *socketio.EventPayload) {
if event.Ack != nil {
Expand All @@ -176,7 +229,9 @@ socket.On("news", func(event *socketio.EventPayload) {
```

#### socket.join(room)

Adds the socket to the given room or to the list of rooms.

```go
io.Of("/test").OnConnection(func(socket *socketio.Socket) {
socket.Join("room 237")
Expand All @@ -186,17 +241,21 @@ io.Of("/test").OnConnection(func(socket *socketio.Socket) {
```

#### socket.leave(room)

Removes the socket from the given room.

```go
io.Of("/test").OnConnection(func(socket *socketio.Socket) {
socket.Leave("room 237");

io.To("room 237").Emit("the user has left the room")
})
```

Rooms are left automatically upon disconnection.

#### socket.to(room)

```go
socket.On("room 237", func(event *socketio.EventPayload) {
// to one room
Expand Down
30 changes: 22 additions & 8 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package main

import (
"fmt"
"log"
"net/http"

socketio "github.com/doquangtan/socket.io/v4"
"github.com/gofiber/fiber/v2"
)

func socketIoRoute(app fiber.Router) {
io := socketio.New()

func socketIoHandle(io *socketio.Io) {
io.OnAuthorization(func(params map[string]string) bool {
// auth, ok := params["Authorization"]
// if !ok {
Expand Down Expand Up @@ -89,18 +89,19 @@ func socketIoRoute(app fiber.Router) {
}
})
})
}

func socketIoRoute(app fiber.Router) {
io := socketio.New()
socketIoHandle(io)
app.Use("/", io.Middleware)
app.Route("/socket.io", io.Server)
}

func main() {
func usingWithGoFiber() {
app := fiber.New(fiber.Config{})

app.Static("/", "./public")

app.Route("/", socketIoRoute)

app.Get("/test", func(c *fiber.Ctx) error {
io := c.Locals("io").(*socketio.Io)

Expand All @@ -114,6 +115,19 @@ func main() {

return c.SendStatus(200)
})
app.Listen(":3400")
}

app.Listen(":3300")
func httpServer() {
io := socketio.New()
socketIoHandle(io)
http.Handle("/socket.io/", io.HttpHandler())
http.Handle("/", http.FileServer(http.Dir("./public")))
fmt.Println("Server listenning on port 3300 ...")
fmt.Println(http.ListenAndServe(":3300", nil))
}

func main() {
httpServer()
// usingWithGoFiber()
}
Loading

0 comments on commit 8e8fdfa

Please sign in to comment.