You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: realtime.go
    path of file: ./repos/pan-light/demo/realtime
    
    the code of the file until where you have to start completion: package realtime

import (
	"bytes"
	"compress/gzip"
	"encoding/json"
	"github.com/pkg/errors"
	"golang.org/x/net/websocket"
	"io/ioutil"
	"log"
	"runtime/debug"
	"sync"
	"time"
)

type gson = map[string]interface{}

type callResult struct {
	ret interface{}
	err error
}

type RealTime struct {
	WsAddr       string
	Role         string
	HostName     string
	HostPassWord string
	SlaveName    string // role 为 slave 时可用
	OnConnected  func()

	inited        bool
	conn          *websocket.Conn
	connectOK     bool
	connectLock   sync.Mutex
	connectOkCond *sync.Cond
	sessionId     string
	sessionSecret string

	listenerMap map[string][]func(data interface{}, room string)
	callMap     sync.Map
	//callMap     map[float64]chan<- *callResult
	callMapLock sync.Mutex
	logWsMsg    bool
}

func (rt *RealTime) Init() {
	if rt.inited {
		return
	}
	rt.inited = true
	rt.connectOkCond = sync.NewCond(&rt.connectLock)
	rt.listenerMap = map[string][]func(data interface{}, room string){}
	go func() {
		for range time.Tic
    
func (rt *RealTime) connect() {
	go func() {
		for {
			rt.connectLock.Lock()
			for rt.connectOK != true { // 等待连接ok
				rt.connectOkCond.Wait()
			}
			rt.connectLock.Unlock()
			if rt.OnConnected != nil {
				go rt.OnConnected()
			}
			log.Println("ws连接成功")
			rt.readLoop()
		}
	}()
	first := true
	for {
		func() { // 等待链接不ok, 进行连接
			rt.connectLock.Lock()
			for rt.connectOK != false {
				rt.connectOkCond.Wait()
			}
			defer func() {
				rt.connectLock.Unlock()
				rt.connectOkCond.Broadcast()
			}()
			if !first {
				time.Sleep(5 * time.Second)
			}
			first = false
			var err error
			conn, err := websocket.Dial(rt.WsAddr, "", "http://localhost/")
			if err != nil {
				log.Println("ws 连接错误", err)
				time.Sleep(5 * time.Second)
				return
			}
			rt.conn = conn
			rt.connectOK = true
			if rt.sessionId != "" {
				log.Println("will resume session")
				err = rt.write(gson{
					"type":          "session.resume",
					"sessionId":     rt.sessionId,
					"sessionSecret": rt.sessionSecret,
				})
			} else {
				log.Println("will new session")
				err = rt.write(gson{
					"type": "session.new",
				})
				err = rt.write(gson{
					"role":        rt.Role,
					"host_name":   rt.HostName,
					"host_secret": rt.HostPassWord,
					"slave_name":  rt.SlaveName,
				})
			}
			if err != nil {
				log.Println("write error", err)
				rt.connectOK = false
				return
			}
		}()
	}
}