README.md 771 Bytes
Newer Older
Jeromy's avatar
Jeromy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#Multistream-select router
This package implements a simple stream router for the multistream-select protocol.
The protocol is defined [here](https://github.com/jbenet/multistream).


Usage:

```go
package main

import (
	"fmt"
	ms "github.com/whyrusleeping/go-multistream"
	"io"
	"net"
)

func main() {
	mux := ms.NewMultistreamMuxer()
	mux.AddHandler("/cats", func(rwc io.ReadWriteCloser) error {
		fmt.Fprintln(rwc, "HELLO I LIKE CATS")
		return rwc.Close()
	})
	mux.AddHandler("/dogs", func(rwc io.ReadWriteCloser) error {
		fmt.Fprintln(rwc, "HELLO I LIKE DOGS")
		return rwc.Close()
	})

	list, err := net.Listen("tcp", ":8765")
	if err != nil {
		panic(err)
	}

	for {
		con, err := list.Accept()
		if err != nil {
			panic(err)
		}

		go mux.Handle(con)
	}
}
```