Skip to content

Latest commit

 

History

History
70 lines (51 loc) · 1.63 KB

README.md

File metadata and controls

70 lines (51 loc) · 1.63 KB

socket.io

GoDoc Build Status

go-socket.io is an implementation of socket.io in golang, which is a realtime application framework.

It is compatible with latest implementation of socket.io in node.js, and supports room and namespace.

  • for compatability with socket.io 0.9.x, please use branch 0.9.x *

Install

Install the package with:

go get github.com/nkovacs/go-socket.io

Import it with:

import "github.com/nkovacs/go-socket.io"

and use socketio as the package name inside the code.

Example

Please check the example folder for details.

package main

import (
	"log"
	"net/http"

	"github.com/nkovacs/go-socket.io"
)

func main() {
	server, err := socketio.NewServer(nil)
	if err != nil {
		log.Fatal(err)
	}
	server.On("connection", func(so socketio.Socket) {
		log.Println("on connection")
		so.Join("chat")
		so.On("chat message", func(msg string) {
			log.Println("emit:", so.Emit("chat message", msg))
			so.BroadcastTo("chat", "chat message", msg)
		})
		so.On("disconnection", func() {
			log.Println("on disconnect")
		})
	})
	server.On("error", func(so socketio.Socket, err error) {
		log.Println("error:", err)
	})

	http.Handle("/socket.io/", server)
	http.Handle("/", http.FileServer(http.Dir("./asset")))
	log.Println("Serving at localhost:5000...")
	log.Fatal(http.ListenAndServe(":5000", nil))
}

License

The 3-clause BSD License - see LICENSE for more details