mirror of
https://github.com/cwinfo/matterbridge.git
synced 2025-06-28 20:49:24 +00:00
Add Gitter support
This commit is contained in:
17
vendor/github.com/mrexodia/wray/examples/client.go
generated
vendored
Normal file
17
vendor/github.com/mrexodia/wray/examples/client.go
generated
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import "github.com/pythonandchips/wray"
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
wray.RegisterTransports([]wray.Transport{&wray.HttpTransport{}})
|
||||
client := wray.NewFayeClient("http://localhost:5000/faye")
|
||||
|
||||
fmt.Println("subscribing")
|
||||
client.Subscribe("/foo", false, func(message wray.Message) {
|
||||
fmt.Println("-------------------------------------------")
|
||||
fmt.Println(message.Data)
|
||||
})
|
||||
|
||||
client.Listen()
|
||||
}
|
15
vendor/github.com/mrexodia/wray/examples/publish.go
generated
vendored
Normal file
15
vendor/github.com/mrexodia/wray/examples/publish.go
generated
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import "github.com/pythonandchips/wray"
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
wray.RegisterTransports([]wray.Transport{ &gofaye.HttpTransport{} })
|
||||
client := wray.NewFayeClient("http://localhost:5000/faye")
|
||||
|
||||
params := map[string]interface{}{"hello": "from golang"}
|
||||
fmt.Println("sending")
|
||||
client.Publish("/foo", params)
|
||||
}
|
||||
|
||||
|
140
vendor/github.com/mrexodia/wray/go_faye.go
generated
vendored
Normal file
140
vendor/github.com/mrexodia/wray/go_faye.go
generated
vendored
Normal file
@ -0,0 +1,140 @@
|
||||
package wray
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
UNCONNECTED = 1
|
||||
CONNECTING = 2
|
||||
CONNECTED = 3
|
||||
DISCONNECTED = 4
|
||||
|
||||
HANDSHAKE = "handshake"
|
||||
RETRY = "retry"
|
||||
NONE = "none"
|
||||
|
||||
CONNECTION_TIMEOUT = 60.0
|
||||
DEFAULT_RETRY = 5.0
|
||||
MAX_REQUEST_SIZE = 2048
|
||||
)
|
||||
|
||||
var (
|
||||
MANDATORY_CONNECTION_TYPES = []string{"long-polling"}
|
||||
registeredTransports = []Transport{}
|
||||
)
|
||||
|
||||
type FayeClient struct {
|
||||
state int
|
||||
url string
|
||||
subscriptions []Subscription
|
||||
transport Transport
|
||||
clientId string
|
||||
schedular Schedular
|
||||
}
|
||||
|
||||
type Subscription struct {
|
||||
channel string
|
||||
callback func(Message)
|
||||
}
|
||||
|
||||
type SubscriptionPromise struct {
|
||||
subscription Subscription
|
||||
}
|
||||
|
||||
func NewFayeClient(url string) *FayeClient {
|
||||
schedular := ChannelSchedular{}
|
||||
client := &FayeClient{url: url, state: UNCONNECTED, schedular: schedular}
|
||||
return client
|
||||
}
|
||||
|
||||
func (self *FayeClient) handshake() {
|
||||
t, err := SelectTransport(self, MANDATORY_CONNECTION_TYPES, []string{})
|
||||
if err != nil {
|
||||
panic("No usable transports available")
|
||||
}
|
||||
self.transport = t
|
||||
self.transport.setUrl(self.url)
|
||||
self.state = CONNECTING
|
||||
handshakeParams := map[string]interface{}{"channel": "/meta/handshake",
|
||||
"version": "1.0",
|
||||
"supportedConnectionTypes": []string{"long-polling"}}
|
||||
response, err := self.transport.send(handshakeParams)
|
||||
if err != nil {
|
||||
fmt.Println("Handshake failed. Retry in 10 seconds")
|
||||
self.state = UNCONNECTED
|
||||
self.schedular.wait(10*time.Second, func() {
|
||||
fmt.Println("retying handshake")
|
||||
self.handshake()
|
||||
})
|
||||
return
|
||||
}
|
||||
self.clientId = response.clientId
|
||||
self.state = CONNECTED
|
||||
self.transport, err = SelectTransport(self, response.supportedConnectionTypes, []string{})
|
||||
if err != nil {
|
||||
panic("Server does not support any available transports. Supported transports: " + strings.Join(response.supportedConnectionTypes, ","))
|
||||
}
|
||||
}
|
||||
|
||||
func (self *FayeClient) Subscribe(channel string, force bool, callback func(Message)) SubscriptionPromise {
|
||||
if self.state == UNCONNECTED {
|
||||
self.handshake()
|
||||
}
|
||||
subscriptionParams := map[string]interface{}{"channel": "/meta/subscribe", "clientId": self.clientId, "subscription": channel, "id": "1"}
|
||||
subscription := Subscription{channel: channel, callback: callback}
|
||||
//TODO: deal with subscription failures
|
||||
self.transport.send(subscriptionParams)
|
||||
self.subscriptions = append(self.subscriptions, subscription)
|
||||
return SubscriptionPromise{subscription}
|
||||
}
|
||||
|
||||
func (self *FayeClient) handleResponse(response Response) {
|
||||
for _, message := range response.messages {
|
||||
for _, subscription := range self.subscriptions {
|
||||
matched, _ := filepath.Match(subscription.channel, message.Channel)
|
||||
if matched {
|
||||
go subscription.callback(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (self *FayeClient) connect() {
|
||||
connectParams := map[string]interface{}{"channel": "/meta/connect", "clientId": self.clientId, "connectionType": self.transport.connectionType()}
|
||||
responseChannel := make(chan Response)
|
||||
go func() {
|
||||
response, _ := self.transport.send(connectParams)
|
||||
responseChannel <- response
|
||||
}()
|
||||
self.listen(responseChannel)
|
||||
}
|
||||
|
||||
func (self *FayeClient) listen(responseChannel chan Response) {
|
||||
response := <-responseChannel
|
||||
if response.successful == true {
|
||||
go self.handleResponse(response)
|
||||
} else {
|
||||
}
|
||||
}
|
||||
|
||||
func (self *FayeClient) Listen() {
|
||||
for {
|
||||
self.connect()
|
||||
}
|
||||
}
|
||||
|
||||
func (self *FayeClient) Publish(channel string, data map[string]interface{}) {
|
||||
if self.state != CONNECTED {
|
||||
self.handshake()
|
||||
}
|
||||
publishParams := map[string]interface{}{"channel": channel, "data": data, "clientId": self.clientId}
|
||||
self.transport.send(publishParams)
|
||||
}
|
||||
|
||||
func RegisterTransports(transports []Transport) {
|
||||
registeredTransports = transports
|
||||
}
|
55
vendor/github.com/mrexodia/wray/http_transport.go
generated
vendored
Normal file
55
vendor/github.com/mrexodia/wray/http_transport.go
generated
vendored
Normal file
@ -0,0 +1,55 @@
|
||||
package wray
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type HttpTransport struct {
|
||||
url string
|
||||
SendHook func(data map[string]interface{})
|
||||
}
|
||||
|
||||
func (self HttpTransport) isUsable(clientUrl string) bool {
|
||||
parsedUrl, err := url.Parse(clientUrl)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if parsedUrl.Scheme == "http" || parsedUrl.Scheme == "https" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (self HttpTransport) connectionType() string {
|
||||
return "long-polling"
|
||||
}
|
||||
|
||||
func (self HttpTransport) send(data map[string]interface{}) (Response, error) {
|
||||
if self.SendHook != nil {
|
||||
self.SendHook(data)
|
||||
}
|
||||
dataBytes, _ := json.Marshal(data)
|
||||
buffer := bytes.NewBuffer(dataBytes)
|
||||
responseData, err := http.Post(self.url, "application/json", buffer)
|
||||
if err != nil {
|
||||
return Response{}, err
|
||||
}
|
||||
if responseData.StatusCode != 200 {
|
||||
return Response{}, errors.New(responseData.Status)
|
||||
}
|
||||
readData, _ := ioutil.ReadAll(responseData.Body)
|
||||
responseData.Body.Close()
|
||||
var jsonData []interface{}
|
||||
json.Unmarshal(readData, &jsonData)
|
||||
response := newResponse(jsonData)
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (self *HttpTransport) setUrl(url string) {
|
||||
self.url = url
|
||||
}
|
61
vendor/github.com/mrexodia/wray/response.go
generated
vendored
Normal file
61
vendor/github.com/mrexodia/wray/response.go
generated
vendored
Normal file
@ -0,0 +1,61 @@
|
||||
package wray
|
||||
|
||||
type Response struct {
|
||||
id string
|
||||
channel string
|
||||
successful bool
|
||||
clientId string
|
||||
supportedConnectionTypes []string
|
||||
messages []Message
|
||||
error error
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
Channel string
|
||||
Id string
|
||||
Data map[string]interface{}
|
||||
}
|
||||
|
||||
func newResponse(data []interface{}) Response {
|
||||
headerData := data[0].(map[string]interface{})
|
||||
messagesData := data[1.:]
|
||||
messages := parseMessages(messagesData)
|
||||
var id string
|
||||
if headerData["id"] != nil {
|
||||
id = headerData["id"].(string)
|
||||
}
|
||||
supportedConnectionTypes := []string{}
|
||||
if headerData["supportedConnectionTypes"] != nil {
|
||||
d := headerData["supportedConnectionTypes"].([]interface{})
|
||||
for _, sct := range(d) {
|
||||
supportedConnectionTypes = append(supportedConnectionTypes, sct.(string))
|
||||
}
|
||||
}
|
||||
var clientId string
|
||||
if headerData["clientId"] != nil {
|
||||
clientId = headerData["clientId"].(string)
|
||||
}
|
||||
return Response{id: id,
|
||||
clientId: clientId,
|
||||
channel: headerData["channel"].(string),
|
||||
successful: headerData["successful"].(bool),
|
||||
messages: messages,
|
||||
supportedConnectionTypes: supportedConnectionTypes}
|
||||
}
|
||||
|
||||
func parseMessages(data []interface{}) []Message {
|
||||
messages := []Message{}
|
||||
for _, messageData := range(data) {
|
||||
m := messageData.(map[string]interface{})
|
||||
var id string
|
||||
if m["id"] != nil {
|
||||
id = m["id"].(string)
|
||||
}
|
||||
message := Message{Channel: m["channel"].(string),
|
||||
Id: id,
|
||||
Data: m["data"].(map[string]interface{})}
|
||||
messages = append(messages, message)
|
||||
}
|
||||
return messages
|
||||
}
|
||||
|
22
vendor/github.com/mrexodia/wray/schedular.go
generated
vendored
Normal file
22
vendor/github.com/mrexodia/wray/schedular.go
generated
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
package wray
|
||||
|
||||
import "time"
|
||||
|
||||
type Schedular interface {
|
||||
wait(time.Duration, func())
|
||||
delay() time.Duration
|
||||
}
|
||||
|
||||
type ChannelSchedular struct {
|
||||
}
|
||||
|
||||
func (self ChannelSchedular) wait(delay time.Duration, callback func()) {
|
||||
go func() {
|
||||
time.Sleep(delay)
|
||||
callback()
|
||||
}()
|
||||
}
|
||||
|
||||
func (self ChannelSchedular) delay() time.Duration {
|
||||
return (1 * time.Minute)
|
||||
}
|
21
vendor/github.com/mrexodia/wray/transport.go
generated
vendored
Normal file
21
vendor/github.com/mrexodia/wray/transport.go
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
package wray
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
type Transport interface {
|
||||
isUsable(string) bool
|
||||
connectionType() string
|
||||
send(map[string]interface{}) (Response, error)
|
||||
setUrl(string)
|
||||
}
|
||||
|
||||
func SelectTransport(client *FayeClient, transportTypes []string, disabled []string) (Transport, error) {
|
||||
for _, transport := range registeredTransports {
|
||||
if contains(transport.connectionType(), transportTypes) && transport.isUsable(client.url) {
|
||||
return transport, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("No usable transports available")
|
||||
}
|
10
vendor/github.com/mrexodia/wray/utils.go
generated
vendored
Normal file
10
vendor/github.com/mrexodia/wray/utils.go
generated
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
package wray
|
||||
|
||||
func contains(target string, slice []string) bool {
|
||||
for _, t := range(slice) {
|
||||
if t == target {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
Reference in New Issue
Block a user