<commit_before>package dispatcher_transport

import (
	"github.com/igm/sockjs-go/sockjs"
	"go.io/auth/transport"
	"go.io/dispatcher/client"
	"go.io/dispatcher/message"
	"log"
	"net/http"
	"time"
)

type SockjsDispatcherTransport struct {
	auth *auth_transport.AuthTransport
}

func NewSockjsDispatcherTransport(auth *auth_transport.AuthTransport) SockjsDispatcherTransport {
	return SockjsDispatcherTransport{auth}
}

func (self *SockjsDispatcherTransport) Listen(messageChannel chan dispatcher_message.Message, clients *dispatcher_client.Clients) {
	sockjs.Install("/sockjs", func(session sockjs.Conn) {
		client := SockjsClient{&session}
		err := self.auth.Authenticate(&client)
		if err != nil {
			clients.Add(client)
			self.ConnectionHandler(session)
		} else {
			log.Println("Dispatcher authentication failed: %s", err)
		}
	}, sockjs.DefaultConfig)
	http.Handle("/", http.FileServer(http.Dir("./www")))
	http_err := http.ListenAndServe(":8080", nil)
	log.Fatal(http_err)
}

func (self *SockjsDispatcherTransport) Destroy() {

}

type SockjsClient struct {
	session *sockjs.Conn
}

func (self *SockjsDispatcherTransport) ConnectionHandler(session sockjs.Conn) {
	log.Println("Client session created: transport=sockjs)")
	for {
		_, err := session.ReadMessage()
		if err != nil {
			break
		}
		go func() { session.WriteMessage([]byte("{\"Id\":\"PING\", \"Body\":\"Ping\", \"Error\":null}")) }()
		time.Sleep(1 * time.Second)
	}<commit_msg>Hooks authentication into sockjs dispatcher transport.<commit_after>package dispatcher_transport

import (
	"github.com/igm/sockjs-go/sockjs"
	"go.io/auth/transport"
	"go.io/dispatcher/client"
	"go.io/dispatcher/message"
	"log"
	"net/http"
	"time"
)

type SockjsDispatcherTransport struct {
	auth auth_transport.AuthTransport
}

func NewSockjsDispatcherTransport(auth *auth_transport.AuthTransport) SockjsDispatcherTransport {
	return SockjsDispatcherTransport{*auth}
}

func (self *SockjsDispatcherTransport) Listen(messageChannel chan dispatcher_message.Message, clients *dispatcher_client.Clients) {
	sockjs.Install("/sockjs", func(session sockjs.Conn) {
		client := SockjsClient{&session}
		c := dispatcher_client.Client(client)
		err := self.auth.Authenticate(&c)
		if err == nil {
			log.Println("Dispatcher client authentication passed: %s", client)
			clients.Add(client)
			self.ConnectionHandler(session)
		} else {
			log.Println("Dispatcher client authentication failed: %s", err)
		}
	}, sockjs.DefaultConfig)
	http.Handle("/", http.FileServer(http.Dir("./www")))
	http_err := http.ListenAndServe(":8080", nil)
	log.Fatal(http_err)
}

func (self *SockjsDispatcherTransport) Destroy() {

}

type SockjsClient struct {
	session *sockjs.Conn
}

func (self *SockjsDispatcherTransport) ConnectionHandler(session sockjs.Conn) {
	log.Println("Client session created: transport=sockjs)")
	for {
		_, err := session.ReadMessage()
		if err != nil {
			break
		}
		go func() { session.WriteMessage([]byte("{\"Id\":\"PING\", \"Body\":\"Ping\", \"Error\":null}")) }()
		time.Sleep(1 * time.Second)
	}
{'fraction_non_alphanumeric': 0.1470298654414178, 'fraction_numerical': 0.0032819166393173614, 'mean_word_length': 5.323651452282157, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4433266', 'n_tokens_mistral': 1092, 'n_tokens_neox': 1010, 'n_words': 186}