Categorygithub.com/djherbis/socket
modulepackage
0.9.1
Repository: https://github.com/djherbis/socket.git
Documentation: pkg.go.dev

# README

socket

GoDoc Software License

Simple Socket.io alternative with #golang server

Usage

Server:

package main

import (
  "fmt"
  "net/http"

  "github.com/djherbis/socket"
)

type MyObject struct{
  Text string `json:"text"`
}

func main() {
  server := socket.NewServer()

  server.On(socket.Connection, func(so socket.Socket) {
    so.Emit("hello", "world")

    so.On("hey", func(msg string) {
      fmt.Println(msg)
    })

    so.On("new obj", func(myobj MyObject){
      fmt.Println(myobj)
    })

    so.Emit("server obj", MyObject{Text: "obj from server to client"})
  })

  router := http.NewServeMux()
  router.Handle("/socket", server)
  router.Handle("/", http.FileServer(http.Dir("."))) // serve up socket.js
  http.ListenAndServe("localhost:8080", router)
}

Client:

<script src="socket.js"></script>
<script>
  var socket = io("localhost:8080/");
  
  socket.emit("hey", "hey there!");
  
  socket.on("hello", function(msg){
    console.log(msg);
  });
  
  socket.emit("new obj", {text: "objects are automatically marshalled/unmarshalled"});

  socket.on("server obj", function(myobj){
    console.log(myobj);
  });
</script>

Installation

# Server Side
go get github.com/djherbis/socket

# Client Side
<script src="socket.js"></script>

# Packages

No description provided by the author

# Functions

No description provided by the author
NewServer creates a new Server.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author

# Structs

Server handles creating Sockets from http connections.

# Interfaces

ClientSocket creates a client-side Socket.
Emitter handles sending messages.
EventHandler registers a function to be run when an event is received.
Handler is both a PacketHandler and a EventHandler.
Namespace is used to multiplex a single Transport and allow independent sockets within a single connection.
Packet is used to receive a sent event and decode the javascript objects into a functions arguments.
PacketHandler responds to a Packet.
Room is a collection of Sockets.
Socket is a Server-side socket.
Transport manages send and receiving Packets.