Git Product home page Git Product logo

nexus's Introduction

Nexus

Codeship Build Status

Nexus is a small websocket server framework.

If in json format, it sends and receives messages as shown in the tags

type Packet struct {
	Type string `json:"type"`
	Data string `json:"data"`
}

Using the delimited format serializes and deserializes faster by simply appending things together.

It sends and receives messages in the format:

# Format
<type_length>:<type><data>

# Examples
5:tokenTOKEN_STRING_GGGGGGGGGGGGGGGG
4:ping
4:chat{"message":"hello"}

Nexus provides a simple interface for registering a handler for each message type:

func main() {

	n := nexus.NewNexus()

	n.Handle("token", handleToken)
	n.Handle("chat", handleChat)

	http.ListenAndServe(":9090", n.Handler)
}

func handleToken(c *nexus.Client, p *nexus.Packet) {		
	user, err := lookupUserByToken(p.Data)
	if err != nil {
		// send a response here
		return
	}

	// store the user in the client's Env for using later
	c.Env["user"] = user
}

func handleChat(c *nexus.Client, p *nexus.Packet) {
	if _, ok := c.Env["user"] !ok {
		// unauthorized
		return
	}

	n.All().Broadcast(p)
}

Cheers! I'll add more later as I think of ways to keep it simple with websockets

nexus's People

Contributors

ds0nt avatar

Forkers

mcolonj

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.