func (v *VoiceConnection) onEvent(message []byte) {
	v.log(LogDebug, "received: %s", string(message))
	var e Event
	if err := json.Unmarshal(message, &e); err != nil {
		v.log(LogError, "unmarshall error, %s", err)
		return
	}
	switch e.Operation {
	case 2:  
		if err := json.Unmarshal(e.RawData, &v.op2); err != nil {
			v.log(LogError, "OP2 unmarshall error, %s, %s", err, string(e.RawData))
			return
		}
		go v.wsHeartbeat(v.wsConn, v.close, v.op2.HeartbeatInterval)
		err := v.udpOpen()
		if err != nil {
			v.log(LogError, "error opening udp connection, %s", err)
			return
		}
		if v.OpusSend == nil {
			v.OpusSend = make(chan []byte, 2)
		}
		go v.opusSender(v.udpConn, v.close, v.OpusSend, 48000, 960)
		if !v.deaf {
			if v.OpusRecv == nil {
				v.OpusRecv = make(chan *Packet, 2)
			}
			go v.opusReceiver(v.udpConn, v.close, v.OpusRecv)
		}
		return
	case 3:  
		return
	case 4:  
		v.Lock()
		defer v.Unlock()
		v.op4 = voiceOP4{}
		if err := json.Unmarshal(e.RawData, &v.op4); err != nil {
			v.log(LogError, "OP4 unmarshall error, %s, %s", err, string(e.RawData))
			return
		}
		return
	case 5:
		if len(v.voiceSpeakingUpdateHandlers) == 0 {
			return
		}
		voiceSpeakingUpdate := &VoiceSpeakingUpdate{}
		if err := json.Unmarshal(e.RawData, voiceSpeakingUpdate); err != nil {
			v.log(LogError, "OP5 unmarshall error, %s, %s", err, string(e.RawData))
			return
		}
		for _, h := range v.voiceSpeakingUpdateHandlers {
			h(v, voiceSpeakingUpdate)
		}
	default:
		v.log(LogDebug, "unknown voice operation, %d, %s", e.Operation, string(e.RawData))
	}
	return
}
wsEvent handles any voice websocket events. This is only called by the
wsListen() function.
package discordgo
import (
	"encoding/binary"
	"encoding/json"
	"fmt"
	"net"
	"strconv"
	"strings"
	"sync"
	"time"
	"github.com/gorilla/websocket"
	"golang.org/x/crypto/nacl/secretbox"
)
type VoiceConnection struct {
	sync.RWMutex
	Debug        bool  
	LogLevel     int
	Ready        bool  
	UserID       string
	GuildID      string
	ChannelID    string
	deaf         bool
	mute         bool
	speaking     bool
	reconnecting bool  
	OpusSend chan []byte   
	OpusRecv chan *Packet  
	wsConn  *websocket.Conn
	wsMutex sync.Mutex
	udpConn *net.UDPConn
	session *Session
	sessionID string
	token     string
	endpoint  string
	close chan struct{}
	connected chan bool
	op4 voiceOP4
	op2 voiceOP2
	voiceSpeakingUpdateHandlers []VoiceSpeakingUpdateHandler
}
type VoiceSpeakingUpdateHandler func(vc *VoiceConnection, vs *VoiceSpeakingUpdate)
func (v *VoiceConnection) Speaking(b bool) (err error) {
	v.log(LogDebug, "called (%t)", b)
	type voiceSpeakingData struct {
		Speaking bool `json:"speaking"`
		Delay    int  `json:"delay"`
	}
	type voiceSpeakingOp struct {
		Op   int               `json:"op"`  
		Data voiceSpeakingData `json:"d"`
	}
	if v.wsConn == nil {
		return fmt.Errorf("no VoiceConnection websocket")
	}
	data := voiceSpeakingOp{5, voiceSpeakingData{b, 0}}
	v.wsMutex.Lock()
	err = v.wsConn.WriteJSON(data)
	v.wsMutex.Unlock()
	v.Lock()
	defer v.Unlock()
	if err != nil {
		v.speaking = false
		v.log(LogError, "Speaking() write json error, %s", err)
		return
	}
	v.speaking = b
	return
}
func (v *VoiceConnection) ChangeChannel(channelID string, mute, deaf bool) (err error) {
	v.log(LogInformational, "called")
	data := voiceChannelJoinOp{4, voiceChannelJoinData{&v.GuildID, &channelID, mute, deaf}}
	v.wsMutex.Lock()
	err = v.session.wsConn.WriteJSON(data)
	v.wsMutex.Unlock()
	if err != nil {
		return
	}
	v.ChannelID = channelID
	v.deaf = deaf
	v.mute = mute
	v.speaking = false
	return
}
func (v *VoiceConnection) Disconnect() (err error) {
	v.Lock()
	if v.sessionID != "" {
		data := voiceChannelJoinOp{4, voiceChannelJoinData{&v.GuildID, nil, true, true}}
		v.session.wsMutex.Lock()
		err = v.session.wsConn.WriteJSON(data)
		v.session.wsMutex.Unlock()
		v.sessionID = ""
	}
	v.Unlock()
	v.Close()
	v.log(LogInformational, "Deleting VoiceConnection %s", v.GuildID)
	v.session.Lock()
	delete(v.session.VoiceConnections, v.GuildID)
	v.session.Unlock()
	return
}
func (v *VoiceConnection) Close() {
	v.log(LogInformational, "called")
	v.Lock()
	defer v.Unlock()
	v.Ready = false
	v.speaking = false
	if v.close != nil {
		v.log(LogInformational, "closing v.close")
		close(v.close)
		v.close = nil
	}
	if v.udpConn != nil {
		v.log(LogInformational, "closing udp")
		err := v.udpConn.Close()
		if err != nil {
			v.log(LogError, "error closing udp connection, %s", err)
		}
		v.udpConn = nil
	}
	if v.wsConn != nil {
		v.log(LogInformational, "sending close frame")
		v.wsMutex.Lock()
		err := v.wsConn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
		v.wsMutex.Unlock()
		if err != nil {
			v.log(LogError, "error closing websocket, %s", err)
		}
		time.Sleep(1 * time.Second)
		v.log(LogInformational, "closing websocket")
		err = v.wsConn.Close()
		if err != nil {
			v.log(LogError, "error closing websocket, %s", err)
		}
		v.wsConn = nil
	}
}
func (v *VoiceConnection) AddHandler(h VoiceSpeakingUpdateHandler) {
	v.Lock()
	defer v.Unlock()
	v.voiceSpeakingUpdateHandlers = append(v.voiceSpeakingUpdateHandlers, h)
}
type VoiceSpeakingUpdate struct {
	UserID   string `json:"user_id"`
	SSRC     int    `json:"ssrc"`
	Speaking bool   `json:"speaking"`
}
type voiceOP4 struct {
	SecretKey [32]byte `json:"secret_key"`
	Mode      string   `json:"mode"`
}
type voiceOP2 struct {
	SSRC              uint32        `json:"ssrc"`
	Port              int           `json:"port"`
	Modes             []string      `json:"modes"`
	HeartbeatInterval time.Duration `json:"heartbeat_interval"`
	IP                string        `json:"ip"`
}
func (v *VoiceConnection) waitUntilConnected() error {
	v.log(LogInformational, "called")
	i := 0
	for {
		v.RLock()
		ready := v.Ready
		v.RUnlock()
		if ready {
			return nil
		}
		if i > 10 {
			return fmt.Errorf("timeout waiting for voice")
		}
		time.Sleep(1 * time.Second)
		i++
	}
}
func (v *VoiceConnection) open() (err error) {
	v.log(LogInformational, "called")
	v.Lock()
	defer v.Unlock()
	if v.wsConn != nil {
		v.log(LogWarning, "refusing to overwrite non-nil websocket")
		return
	}
	i := 0
	for {
		if v.sessionID != "" {
			break
		}
		if i > 20 {  
			return fmt.Errorf("did not receive voice Session ID in time")
		}
		time.Sleep(50 * time.Millisecond)
		i++
	}
	vg := "wss://" + strings.TrimSuffix(v.endpoint, ":80")
	v.log(LogInformational, "connecting to voice endpoint %s", vg)
	v.wsConn, _, err = websocket.DefaultDialer.Dial(vg, nil)
	if err != nil {
		v.log(LogWarning, "error connecting to voice endpoint %s, %s", vg, err)
		v.log(LogDebug, "voice struct: %#v\n", v)
		return
	}
	type voiceHandshakeData struct {
		ServerID  string `json:"server_id"`
		UserID    string `json:"user_id"`
		SessionID string `json:"session_id"`
		Token     string `json:"token"`
	}
	type voiceHandshakeOp struct {
		Op   int                `json:"op"`  
		Data voiceHandshakeData `json:"d"`
	}
	data := voiceHandshakeOp{0, voiceHandshakeData{v.GuildID, v.UserID, v.sessionID, v.token}}
	err = v.wsConn.WriteJSON(data)
	if err != nil {
		v.log(LogWarning, "error sending init packet, %s", err)
		return
	}
	v.close = make(chan struct{})
	go v.wsListen(v.wsConn, v.close)
	return
}
func (v *VoiceConnection) wsListen(wsConn *websocket.Conn, close <-chan struct{}) {
	v.log(LogInformational, "called")
	for {
		_, message, err := v.wsConn.ReadMessage()
		if err != nil {
			if websocket.IsCloseError(err, 4014) {
				v.log(LogInformational, "received 4014 manual disconnection")
				v.Lock()
				v.wsConn = nil
				v.Unlock()
				v.session.Lock()
				delete(v.session.VoiceConnections, v.GuildID)
				v.session.Unlock()
				v.Close()
				return
			}
			v.RLock()
			sameConnection := v.wsConn == wsConn
			v.RUnlock()
			if sameConnection {
				v.log(LogError, "voice endpoint %s websocket closed unexpectantly, %s", v.endpoint, err)
				go v.reconnect()
			}
			return
		}
		select {
		case <-close:
			return
		default:
			go v.onEvent(message)
		}
	}
}