<commit_before>// Package socketio implements a client for SocketIO protocol
// as specified in https://github.com/LearnBoost/socket.io-spec
package socketio

import (
	"encoding/json"
	"time"
)

type Event struct {
	Name string
	Args interface{}
}

type Socket struct {
	Url       string
	Channel   string
	Session   *Session
	Transport *Transport

	OnMessageFuncs []OnMessageFunc
	OnEventFuncs   []OnEventFunc
	OnJSONFuncs    []OnJSONFunc
}

type OnDisconnectFunc func(channel string)

type OnConnectFunc func(endpoint Endpoint)

type OnHeartbeatFunc func()

type OnMessageFunc func(msg string)

type OnEventFunc func(event string, args interface{})

type OnJSONFunc func(jsonStr string)

type OnACKFunc func(id string, data string)

type OnErrorFunc func(channel, reason, advice string)

type OnNoopFunc func()

func NewSocket(url string, channel string) (*Socket, error) {
	session, err := NewSession(url)
	if err != nil {
		return nil, err
	}

	transport, err := NewTransport(session, url, channel)
	if err != nil {
		return nil, err
	}

	return &Socket{url, channel, session, transport, nil, nil, nil}, nil
}

// TODO: Mux and Demux multiple channels on same transport and socket
func (socket *Socket) Dial() {
	// Connect
	endpoint := NewEndpoint(socket.Channel, "")
	connectMsg := NewConnect(endpoint)
	socket.Transport.Send(connectMsg)

	// Heartbeat goroutine
	go func() {
		heartbeatMsg := NewHeartbeat()
		for {
			time.Sleep(time.Duration(socket.Session.HeartbeatTimeout-1) * time.Second)
			_ = socket.Transport.Send(heartbeatMsg)
		}
	}()

	// Message receiving loop
	for {
		msg, err := socket.Transport.Receive()
		if err != nil {
			println(err.Error())
			continue
		}

		switch msg.Type {
		case 3:
			for _, onMessageFunc := range socket.OnMessageFuncs {
				go onMessageFunc(msg.Data)
			}
		case 5:
			var event Event
			err := json.Unmarshal([]byte(msg.Data), &event)
			if err != nil {
				panic(err)
			}

			for _, onEventFunc := range socket.OnEventFuncs {
				go onEventFunc(event.Name, event.Args)
			}
		default:
			println("Received a message " + string(msg.Type))
		}
	}
}

func (socket *Socket) OnMessage(omf OnMessageFunc) {
	socket.OnMessageFuncs = append(socket.OnMessageFuncs, omf)
}

func (socket *Socket) OnJSON(ojf OnJSONFunc) {
	socket.OnJSONFuncs = append(socket.OnJSONFuncs, ojf)
}

func (socket *Socket) OnEvent(oef OnEventFunc) {
	socket.OnEventFuncs = append(socket.OnEventFuncs, oef)
}
<commit_msg>Don't use callbacks for receiving messages<commit_after>// Package socketio implements a client for SocketIO protocol
// as specified in https://github.com/LearnBoost/socket.io-spec
package socketio

import (
	"time"
)

type Event struct {
	Name string
	Args interface{}
}

type Socket struct {
	Url       string
	Channel   string
	Session   *Session
	Transport *Transport
}

func Dial(url string, channel string) (*Socket, error) {
	session, err := NewSession(url)
	if err != nil {
		return nil, err
	}

	transport, err := NewTransport(session, url, channel)
	if err != nil {
		return nil, err
	}

	// Connect
	endpoint := NewEndpoint(channel, "")
	connectMsg := NewConnect(endpoint)
	transport.Send(connectMsg)

	// Heartbeat goroutine
	go func() {
		heartbeatMsg := NewHeartbeat()
		for {
			time.Sleep(time.Duration(session.HeartbeatTimeout-1) * time.Second)
			_ = transport.Send(heartbeatMsg)
		}
	}()

	return &Socket{url, channel, session, transport}, nil
}

func (socket *Socket) Receive() (*IOMessage, error) {
Begining:
  msg, err := socket.Transport.Receive()
  if err != nil {
    return nil, err
  }

  switch msg.Type {
  case 3, 5:
    return msg, nil
  default:
    goto Begining
  }
}


{'fraction_non_alphanumeric': 0.1126027397260274, 'fraction_numerical': 0.0016438356164383563, 'mean_word_length': 3.907258064516129, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4167336', 'n_tokens_mistral': 1281, 'n_tokens_neox': 1193, 'n_words': 335}