项目作者: oimyounis

项目描述 :
Event-driven server/client TCP sockets
高级语言: Go
项目地址: git://github.com/oimyounis/go-sockets.git
创建时间: 2019-11-07T23:32:18Z
项目社区:https://github.com/oimyounis/go-sockets

开源协议:BSD 3-Clause "New" or "Revised" License

下载


go-sockets

go-sockets is an event-driven TCP sockets framework that allows you to create a server/client architecture with ease based on named events.

Project Status

go-sockets is currently in an early development stage and is not ready for production use yet.

Installation

Dependencies

Execute the following command in your Terminal:

  1. go get -u github.com/google/uuid

Download go-sockets

Execute the following command in your Terminal:

  1. go get -u github.com/oimyounis/go-sockets

Hello World

Building The Server

main.go

  1. Import go-sockets server

    1. import (
    2. // ...
    3. "github.com/oimyounis/go-sockets/server"
    4. // ...
    5. )
  2. Initialize a new server instance

    1. srv := server.New(":8000")

    New takes a single argument, address that the server will listen on in the format \:\. For example: 127.0.0.1:9000.

  3. Set the OnConnection event handler

    1. srv.OnConnection(func(socket *server.Socket) {
    2. log.Printf("socket connected with id: %v\n", socket.Id)
    3. socket.On("ping", func(data string) {
    4. log.Println("message received on event: ping: " + data)
    5. socket.Send("pong", fmt.Sprintf("%v", time.Now().Unix()))
    6. })
    7. })

    Here we setup an event handler that will fire when a client connects to us then we listen for the ping event and then we send back a pong event with the current time in seconds.

Important: the OnConnect event is a blocking call, so if you have blocking code that does not need to finish before the server starts processing the client’s messages you should call it inside a goroutine.

  1. Start the server
    ```go
    err := srv.Listen()

if err != nil {
log.Fatalf(“Failed to start server: %v\n”, err)
}

  1. ***Listen*** blocks the current thread listening for connections.
  2. ### Building The Client
  3. **main.go**
  4. 1. Import go-sockets client
  5. ```go
  6. import (
  7. // ...
  8. "github.com/oimyounis/go-sockets/client"
  9. // ...
  10. )
  1. Initialize a new client instance

    1. socket := client.New("localhost:8000")

    Same as with the server, New takes a single argument, address that points to the server’s address in the format \:\. For example: 127.0.0.1:9000.

  2. Set event handlers
    ```go
    socket.On(“connection”, func(_ string) {
    log.Println(“connected to server”)

    go func() {

    1. for {
    2. socket.Send("ping", fmt.Sprintf("%v", time.Now().Unix()))
    3. time.Sleep(time.Second)
    4. }

    }()
    })

socket.On(“pong”, func(data string) {
log.Printf(“pong:%v\n”, data)
})

  1. Here we setup an event handler that will fire when the client connects to the server and start a goroutine that loops forever sending a ***ping*** event to the server every second then we setup a listener for the ***pong*** event.
  2. **Important**: notice that we started the loop in a goroutine. That's because the **connection** event is a blocking call, so if you have blocking code that does not need to finish before the client starts listening on the connection you should call it inside a goroutine.
  3. 4. Start the client
  4. ```go
  5. err := socket.Listen()
  6. if err != nil {
  7. log.Fatalf("Couldn't connect to server: %v\n", err)
  8. }

Listen blocks the current thread listening for data.

License

Licensed under the New BSD License.

Contribution

All contributions are welcome.
You are very welcome to submit a new feature, fix a bug or an optimization to the code.

To Contribute:

  1. Fork this repo.
  2. Create a new branch with a descriptive name (example: feature/some-new-function or bugfix/fix-something-somewhere).
  3. Commit and push your code to your new branch.
  4. Create a new Pull Request here.