• Stars
    star
    165
  • Rank 228,906 (Top 5 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 7 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

Gorilla websockets based simplified websocket-client implementation in GO.

GoWebsocket

Gorilla websocket based simplified client implementation in GO.

Overview

This client provides following easy to implement functionality

  • Support for emitting and receiving text and binary data
  • Data compression
  • Concurrency control
  • Proxy support
  • Setting request headers
  • Subprotocols support
  • SSL verification enable/disable

To install use

    go get github.com/sacOO7/gowebsocket

Description

Create instance of Websocket by passing url of websocket-server end-point

    //Create a client instance
    socket := gowebsocket.New("ws://echo.websocket.org/")
    

Important Note : url to websocket server must be specified with either ws or wss.

Connecting to server

  • For connecting to server:
    //This will send websocket handshake request to socketcluster-server
    socket.Connect()

Registering All Listeners

    package main
    
    import (
    	"log"
    	"github.com/sacOO7/gowebsocket"
        "os"
        "os/signal"
    )
    
    func main() {
    
        interrupt := make(chan os.Signal, 1)
        signal.Notify(interrupt, os.Interrupt)
        
    	socket := gowebsocket.New("ws://echo.websocket.org/");
    	
    	socket.OnConnected = func(socket gowebsocket.Socket) {
    		log.Println("Connected to server");
    	};
    	
        socket.OnConnectError = func(err error, socket gowebsocket.Socket) {
            log.Println("Received connect error ", err)
        };
        
    	socket.OnTextMessage = func(message string, socket gowebsocket.Socket) {
    		log.Println("Received message " + message)
    	};
    	
    	socket.OnBinaryMessage = func(data [] byte, socket gowebsocket.Socket) {
            log.Println("Received binary data ", data)
        };
        
    	socket.OnPingReceived = func(data string, socket gowebsocket.Socket) {
    		log.Println("Received ping " + data)
    	};
    	
    	socket.OnPongReceived = func(data string, socket gowebsocket.Socket) {
            log.Println("Received pong " + data)
        };
        
    	socket.OnDisconnected = func(err error, socket gowebsocket.Socket) {
    		log.Println("Disconnected from server ")
    		return
    	};
    	
    	socket.Connect()
    	
        for {
            select {
            case <-interrupt:
                log.Println("interrupt")
                socket.Close()
                return
            }
        }
    }
    

Sending Text message

    socket.SendText("Hi there, this is my sample test message")

Sending Binary data

    token := make([]byte, 4)
    // rand.Read(token) putting some random value in token
    socket.SendBinary(token)

Closing the connection with server

    socket.Close()

Setting request headers

	socket.RequestHeader.Set("Accept-Encoding","gzip, deflate, sdch")
	socket.RequestHeader.Set("Accept-Language","en-US,en;q=0.8")
	socket.RequestHeader.Set("Pragma","no-cache")
	socket.RequestHeader.Set("User-Agent","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36")
	

Setting proxy server

  • It can be set using connectionOptions by providing url to proxy server
    socket.ConnectionOptions = gowebsocket.ConnectionOptions {
       Proxy: gowebsocket.BuildProxy("http://example.com"),
    }

Setting data compression, ssl verification and subprotocols

  • It can be set using connectionOptions inside socket
    socket.ConnectionOptions = gowebsocket.ConnectionOptions {
        UseSSL:true,
        UseCompression:true,
        Subprotocols: [] string{"chat","superchat"},
    }
  • ConnectionOptions needs to be applied before connecting to server
  • Please checkout examples/gowebsocket directory for detailed code..

License

Apache License, Version 2.0

More Repositories

1

socketcluster-client-java

Native java and android client for socketcluster framework in node.js
Java
94
star
2

SocketclusterClientDotNet

C# client for socketcluster framework in node.js
C#
69
star
3

socketcluster-client-go

GO client for socketcluster
Go
55
star
4

socketcluster-client-python

Python client for socket-cluster framework in node.js
Python
49
star
5

socketcluster-client-unity

An extension to original C# client implementation.
C#
24
star
6

socketcluster-client-swift

Native iOS/macOS client written in swift 4/swift 5
Swift
22
star
7

socketcluster-client-C

C/ C++ client for socketcluster framework in node.js
C
13
star
8

socketcluster-android-demo

Sample chat app demo built using socketcluster framework in node.js
Java
12
star
9

OpenSubtitle-API

An API implemented in java for downloading subtitles using XML-RPC protocol
Java
8
star
10

LAN-chat-Web-App

A simple chat web app based on SocketCluster framework in Nodejs :) .
JavaScript
5
star
11

Parking-Lot-System

Simple parking lot system for SL4
Java
4
star
12

selenium-js-extensions

Selenium safe JS bindings for executing user actions.
TypeScript
4
star
13

RocketChat-Android-Demo

Android demo for RocketChat built using native java SDK.
Java
3
star
14

locust-httpx-testing

Locust testing using httpx as a client
Python
3
star
15

Spamchat

A simple chat client for instant messaging using pushy API.
C
2
star
16

CollegeFinder

A web app for students to find eligible colleges based on JEE AIR rank. (MiniProject TE :p)
HTML
2
star
17

socketcluster-client-testing

A sample repo. for testing socketcluster server using gradle script
Java
2
star
18

ably-go-testing

Testing ably-go message send and receive
Go
1
star
19

go-logger

Lightweight extension to internal go logging library (based on python logging https://docs.python.org/2/library/logging.html)
Go
1
star
20

Rule-Editor

A rule editor desktop app for js rules
CSS
1
star
21

Jfoenix-components

Desktop app for all material UI components in Javafx
Java
1
star
22

LyricsFinder-API

An API implemented in java for downloading lyrics. (Can be used in android)
Java
1
star
23

SCIM-Okta-Integration

Repo. to Test SCIM server integration with OKTA
JavaScript
1
star
24

IpGeoLocator

Find location of server or client using ip address or hostname
Python
1
star
25

socketcluster-testing

a sample repo for testing
Java
1
star
26

ldex-market-discovery

Ldex client module to retrieve active market data based on blockchain transactions
JavaScript
1
star
27

Pytest-BDD-Samples

Learning python behaviour testing using Pytest-BDD framework
Python
1
star