• Stars
    star
    53
  • Rank 552,529 (Top 11 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 4 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ‡ CLI tool for websockets and Go package

Hare Sockets πŸ‡

Go codecov Mentioned in Awesome Go Go Report Card Codacy Badge Maintainability GoDoc Join the chat at https://gitter.im/hare-sockets/community Release License: MIT

Hare is an user-friendly package for sockets in Golang and a CLI tool for sockets interaction. You can send and listen to TCP connections with a few lines of code or commands.

Contents

πŸ–₯️ Installation

Installation guide for the CLI Tool and Golang Library.

πŸ’» CLI Tool

To install the CLI tool, you can install it through Homebrew:

$ brew tap leozz37/hare

$ brew install hare

Or you can install manually with the Makefile script:

$ make install

πŸ‡ Golang Lib

First, you need Go (version 1.12+ is required), then you can install Hare:

$ go get -u "github.com/leozz37/hare"

Import it in your code:

import "github.com/leozz37/hare"

πŸ• Quickstart

Quick start for the CLI Tool and the Golang Library.

πŸ’» CLI Tool

To use the CLI tool, these are the flags:

  -d string
        Data to be sended
  -h string
        Host address to bo operated (default "localhost")
  -l    Listen to a given address
  -p string
        Port address to bo operated         [REQUIRED]
  -s    Send message to a given address

You can run the --help flag:

$ hare --help

To Listen to port 3000 for example, run:

$ hare -l -p 3000

To Send a payload with the message Hello World to port 3000 for example, run:

$ hare -s -p 3000 -d 'Hello World'

cli-example

πŸ‡ Golang Lib

Sample code for sending payloads:

package main

import (
    "github.com/leozz37/hare"
)

func main() {
    hare.Send(3000, "Hello, World")
}

Sample code for listening a port:

package main

import (
    "fmt"

    "github.com/leozz37/hare"
)

func main() {
    r, _ := hare.Listen("3000")

    for {
        if r.HasNewMessages() {
            fmt.Println(r.GetMessage())
        }
    }
}

πŸ“– Documentation

The library consists of two features: listen and send to a given port. You can check the full documentation on Godoc.

Send

Receives a port and a message, both as string and returns an error (if something goes wrong).

func Send(port, message string) error;

Usage example:

func main() {
    err := hare.Send(3000, "Hello, World")
    if err != nil {
        panic(err)
    }
}

Listen

Receives a port as string and returns a Listener struct and an error (if something goes wrong).

func Listen(port string) (*Listener, error);

Usage example:

func main() {
    r, _ := hare.Listen("3000")
    l, _ := hare.listen("3001")

    for {
        if r.HasNewMessages() {
            fmt.Println(r.GetMessage())
        } else if l.HasNewMessages() {
            fmt.Println(l.GetMessage())
        }
    }

Listener

The Listener struct returned by Listen() function has the following fields:

type Listener struct {
    SocketListener net.Listener
    HasNewMessages func() bool
    GetMessage     func() string
    Stop           func()
}

SocketListener is the socket connection.

listener.SocketListener, _ = net.Listen("tcp", "localhost:" + port)

HasNewMessages() function returns a bool being true with there's a new message:

func main() {
    r, _ := hare.Listen("3000")

    if r.HasNewMessages() {
        fmt.Println("There's a new message!")
    }
}

GetMessage() function returns a string with the last message received on the socket:

func main() {
    r, _ := hare.Listen("3000")

    if r.HasNewMessages() {
        fmt.Println(r.GetMessage())
    }
}

Stop() function closes the listener connection:

func main() {
    r, _ := hare.Listen("3000")
    hare.Send("3000", "Hey beauty")

    r.Stop()

    err := Send("3000", "This should fails")
    if err != nil {
        panic(err)
    }
}

πŸ“™ Examples

You can check the example for code usages, like send and listen samples.

Since Hare only listens and send messages, here's a complete example:

package main

import (
    "fmt"
    "time"

    "github.com/leozz37/hare"
)

func listenSockets(port string) {
    r, _ := hare.Listen(port)

    for {
        if r.HasNewMessages() {
            fmt.Println(r.GetMessage())
        }
    }
}

func main() {
    go listenSockets("3000")
    go listenSockets("3001")

    for {
        hare.Send("3000", "Hello port 3000")
        hare.Send("3001", "Hello port 3001")
        time.Sleep(time.Second)
    }
}

πŸ§ͺ Testing

To run the test suite, you can run with:

$ go test

If you want a more detailed report with coverage and an coverage.out file, do the following:

$ go test -v -covermode=count -coverprofile=coverage.out

🀝 Contributing

A full guideline about contributing to Hare can be found in the CONTRIBUTING.md file.

βš–οΈ License

Hare is released under the MIT License.

More Repositories

1

golang-elk-example

This is an example integrating a Golang code to ELK stack
Go
21
star
2

Texugo

🦑 Fast, flexible, multiplatform, lightweight and, dependency-free message gateway
C++
18
star
3

texugo

🦑 Fast, flexible, multiplatform, lightweight and, dependency-free message gateway
C++
17
star
4

jaguar

πŸ† CLI tool for testing socket connections
Rust
9
star
5

books

8
star
6

gin-serverless-container-template

πŸš€ This is a template for web apps using Golang, Gin, Docker and Google Cloud Run
HCL
8
star
7

GoogleCloudPlatform4noobs

☁️ Introdução aos recursos da Google Cloud Platform da He4rt Developers!
6
star
8

kangaroo

🦘 User-friendly socket lib for Python
Python
5
star
9

gin-app-template

πŸš€ This is a template for web apps using Golang, Gin, Docker and Kubernetes
HCL
5
star
10

video-downloader

πŸ“Ί Download videos from YouTube, Facebook, Twitter and Instagram!
Go
5
star
11

iot-monitoring-gcp-grafana

Example using Gin Framework, Prometheus, Grafana, and Google Cloud Platform.
Go
5
star
12

leozz37

JavaScript
4
star
13

cloud-run-actions-example

🐳 Creating a CI/CD Environment for Serverless Containers on Google Cloud Run
Go
4
star
14

cicd-project-starter

πŸš€ A Template for Continuous Integration and Continuous Deployment on AWS
Shell
3
star
15

docker-unit-tests

Some examples on how to use Docker to run unit tests
Python
1
star
16

rust-rest-api

Rest API made with Rocket.
Rust
1
star
17

AWS-Certification-Practice

AWS Certification Practice Tests
1
star
18

chat-message

Simple Python chat message
Python
1
star
19

spaceapps-covid

1
star
20

url-scrapper

🦊 Download all the images from a website
Python
1
star
21

gtest-and-cmake-example

A simple example of Google Test Framework and CMake usage
CMake
1
star
22

glucose-measure-api

Integration with Librelink Freestyle, MiaoMiao, Golang and Google Cloud
Go
1
star