2 * Copyright (C) 2018 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
25 socketio_client "github.com/sebd71/go-socket.io-client"
29 type IOSockClient struct {
31 Conn *socketio_client.Client
32 Options *socketio_client.Options
38 // NewIoSocketClient Create a new IOSockClient
39 func NewIoSocketClient(url, clientID string) (*IOSockClient, error) {
43 sCli := &IOSockClient{
45 EmitMutex: &sync.Mutex{},
46 Options: &socketio_client.Options{
47 Transport: "websocket",
48 Header: make(map[string][]string),
51 sCli.Options.Header["XDS-AGENT-SID"] = []string{clientID}
53 sCli.Conn, err = socketio_client.NewClient(url, sCli.Options)
55 return nil, fmt.Errorf("IO.socket connection error: " + err.Error())
58 sCli.Conn.On("connection", func() {
62 sCli.Conn.On("disconnection", func(err error) {
63 Log.Debugf("WS disconnection event with err: %v\n", err)
64 sCli.Connected = false
70 // On record a callback on a specific message
71 func (c *IOSockClient) On(message string, f interface{}) (err error) {
72 return c.Conn.On(message, f)
75 // Emit send a message
76 func (c *IOSockClient) Emit(message string, args ...interface{}) (err error) {
78 defer c.EmitMutex.Unlock()
79 return c.Conn.Emit(message, args...)