Git Product home page Git Product logo

websocket-conn's Introduction

websocket-conn Build Status

๐Ÿ“ž A dead simple WebSocket connection written in Go.

websocket-conn provides you with easy handling for WebSockets, it's based on github.com/gorilla/websocket.

Installation

$ go get -u github.com/shiwano/websocket-conn/v4

Usage

func Connect(ctx context.Context, settings Settings, url string, requestHeader http.Header) (*Conn *http.Response, error)
func UpgradeFromHTTP(ctx context.Context, settings Settings, w http.ResponseWriter, r *http.Request) (*Conn, error)

type Conn struct {
  Stream() <-chan Message
  Err() error
  SendBinaryMessage(data []byte) error
  SendTextMessage(text string) error
  SendJSONMessage(v interface{}) error
  Close() error
}

Examples

Server:

package main

import (
  "context"
  "net/http"
  wsconn "github.com/shiwano/websocket-conn/v4"
)

func main() {
  http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    c, _ := wsconn.UpgradeFromHTTP(r.Context(), wsconn.DefaultSettings(), w, r)

    for m := range c.Stream() {
      switch t := m.Text(); t {
      case "Hello":
        if err := c.SendTextMessage(t + " World"); err != nil {
          log.Fatal(err)
        }
      case "Close":
        c.Close()
      }
    }
  })
  http.ListenAndServe(":5000", nil)
}

Client:

package main

import (
  "context"
  "log"
  wsconn "github.com/shiwano/websocket-conn/v4"
)

func main() {
  ctx, cancel := context.WithCancel(context.Background())
  defer cancel()

  c, _, _ := wsconn.Connect(ctx, wsconn.DefaultSettings(), "ws://localhost:5000", nil)

  c.SendTextMessage("Hello")
  m := <-c.Stream()
  log.Println(m.Text()) // Output: Hello World
  c.SendTextMessage("Close")

  for range c.Stream() {
    // wait for closing.
  }
}

See also examples directory.

License

Copyright (c) 2016 Shogo Iwano Licensed under the MIT license.

websocket-conn's People

Contributors

shiwano avatar

Stargazers

trasta avatar Doru Carastan avatar Serge Simard avatar  avatar Ola avatar Wei Wuxian avatar Timesking avatar ใ‚‚ใใ‚ณใƒญ avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.