Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
zishang520 committed Jul 16, 2022
0 parents commit 18bc616
Show file tree
Hide file tree
Showing 29 changed files with 7,958 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor
/bin/*
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

MIT License

Copyright (c) 2022 luoyy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
141 changes: 141 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# socket.io

## Features

Socket.IO enables real-time bidirectional event-based communication. It consists of:

- a Golang server (this repository)
- a [Javascript client library](https://github.com/socketio/socket.io-client) for the browser (or a Node.js client)

Some implementations in other languages are also available:

- [Java](https://github.com/socketio/socket.io-client-java)
- [C++](https://github.com/socketio/socket.io-client-cpp)
- [Swift](https://github.com/socketio/socket.io-client-swift)
- [Dart](https://github.com/rikulo/socket.io-client-dart)
- [Python](https://github.com/miguelgrinberg/python-socketio)
- [.NET](https://github.com/doghappy/socket.io-client-csharp)

Its main features are:

#### Reliability

Connections are established even in the presence of:
- proxies and load balancers.
- personal firewall and antivirus software.

For this purpose, it relies on [Engine.IO for golang](https://github.com/zishang520/engine.io), which first establishes a long-polling connection, then tries to upgrade to better transports that are "tested" on the side, like WebSocket. Please see the [Goals](https://github.com/zishang520/engine.io#goals) section for more information.

#### Auto-reconnection support

Unless instructed otherwise a disconnected client will try to reconnect forever, until the server is available again. Please see the available reconnection options [here](https://socket.io/docs/v3/client-api/#new-Manager-url-options).

#### Disconnection detection

A heartbeat mechanism is implemented at the Engine.IO level, allowing both the server and the client to know when the other one is not responding anymore.

That functionality is achieved with timers set on both the server and the client, with timeout values (the `pingInterval` and `pingTimeout` parameters) shared during the connection handshake. Those timers require any subsequent client calls to be directed to the same server, hence the `sticky-session` requirement when using multiples nodes.

#### Binary support

Any serializable data structures can be emitted, including:

- []byte and io.Reader


#### Simple and convenient API

Sample code:

```golang
import (
"github.com/zishang520/socket.io/socket"
)
io.On("connection", func(clients ...interface{}) {
client := clients[0].(*socket.Socket)
client.Emit("request" /**/) // emit an event to the socket
io.Emit("broadcast" /**/) // emit an event to all connected sockets
client.On("reply", func(...interface{}) { /**/ }) // listen to the event
})
```

#### Multiplexing support

In order to create separation of concerns within your application (for example per module, or based on permissions), Socket.IO allows you to create several `Namespaces`, which will act as separate communication channels but will share the same underlying connection.

#### Room support

Within each `Namespace`, you can define arbitrary channels, called `Rooms`, that sockets can join and leave. You can then broadcast to any given room, reaching every socket that has joined it.

This is a useful feature to send notifications to a group of users, or to a given user connected on several devices for example.


**Note:** Socket.IO is not a WebSocket implementation. Although Socket.IO indeed uses WebSocket as a transport when possible, it adds some metadata to each packet: the packet type, the namespace and the ack id when a message acknowledgement is needed. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a WebSocket server (like `ws://echo.websocket.org`) either. Please see the protocol specification [here](https://github.com/socketio/socket.io-protocol).


## How to use

The following example attaches socket.io to a plain engine.io *types.CreateServer listening on port `3000`.
```golang
package main

import (
"github.com/zishang520/engine.io/types"
"github.com/zishang520/engine.io/utils"
"github.com/zishang520/socket.io/socket"
"os"
"os/signal"
"syscall"
)

func main() {
utils.Log().DEBUG = true
httpServer := types.CreateServer(nil)
io := socket.NewServer(httpServer, nil)
io.On("connection", func(clients ...interface{}) {
client := clients[0].(*socket.Socket)
client.On("event", func(datas ...interface{}) {
})
client.On("disconnect", func(...interface{}) {
})
})
httpServer.Listen("127.0.0.1:3000", nil)

exit := make(chan struct{})
SignalC := make(chan os.Signal)

signal.Notify(SignalC, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
for s := range SignalC {
switch s {
case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
close(exit)
return
}
}
}()

<-exit
httpServer.Close(nil)
os.Exit(0)
}
```

## Documentation

Not available yet

## Debug / logging

Not available yet

## Testing

Not available yet




## License

[MIT](LICENSE)
7 changes: 7 additions & 0 deletions client-dist/socket.io.esm.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions client-dist/socket.io.esm.min.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 18bc616

Please sign in to comment.