本文整理匯總了Golang中code/google/com/p/go/net/websocket.Conn.PayloadType方法的典型用法代碼示例。如果您正苦於以下問題:Golang Conn.PayloadType方法的具體用法?Golang Conn.PayloadType怎麽用?Golang Conn.PayloadType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類code/google/com/p/go/net/websocket.Conn
的用法示例。
在下文中一共展示了Conn.PayloadType方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: FrameServer
// Write received messages from a channel to the websocket client
func FrameServer(conn *websocket.Conn) {
// Set the PayloadType to binary
conn.PayloadType = websocket.BinaryFrame
client := NewClient()
registerClient <- client
defer func() {
unregisterClient <- client
}()
// Write the header as the first message to the client (MPEG size etc.)
conn.SetWriteDeadline(time.Now().Add(ClientWriteTimeout * time.Second))
if _, err := conn.Write(*headerMessage); err != nil {
return
}
// Continuously read messages from the queue and write to the client, stop on error
for msg := range client.queue {
conn.SetWriteDeadline(time.Now().Add(ClientWriteTimeout * time.Second))
if _, err := conn.Write(*msg); err != nil {
return
}
}
}
示例2: Handle
func (endpoint *WebsocketEndpoint) Handle(ws *websocket.Conn) {
ws.PayloadType = websocket.BinaryFrame
var buf = make([]byte, 1024*64)
var sessConn = &WebsocketSessionConnection{
conn: ws,
}
var session Session = endpoint.context.CreateSession(sessConn)
fmt.Printf("New session: %s\n", session.ID())
for {
msgLength, err := ws.Read(buf)
if err != nil {
if err != io.EOF {
fmt.Printf("WS error: %#v\n", err)
}
fmt.Printf("Session closed: %s\n", session.ID())
break
}
if msgLength == 0 {
continue
}
var msgBuf = make([]byte, msgLength)
copy(msgBuf, buf)
endpoint.Handler(endpoint, msgBuf, session, ws)
}
}
示例3: wshandler
func wshandler(ws *websocket.Conn) {
flag.Parse()
var mqcon net.Conn
var err error
if *bs {
conf := tls.Config{InsecureSkipVerify: *bsinsec}
if *bcert != "" && *bkey != "" {
Cert, err := tls.LoadX509KeyPair(*bcert, *bkey)
if err != nil {
fmt.Println("LoadX509KeyPair:", err)
return
}
conf.Certificates = []tls.Certificate{Cert}
}
mqcon, err = tls.Dial("tcp", *bhost+":"+*bport, &conf)
} else {
mqcon, err = net.Dial("tcp", *bhost+":"+*bport)
}
if err != nil {
fmt.Println("mqcon error:", err.Error())
ws.Close()
return
}
ws.PayloadType = websocket.BinaryFrame
bmqcon := bufio.NewReadWriter(bufio.NewReader(mqcon), bufio.NewWriter(mqcon))
bws := bufio.NewReadWriter(bufio.NewReader(ws), bufio.NewWriter(ws))
go func() {
for {
msg, err := mqtt.DecodeOneMessage(bmqcon, nil)
// fmt.Println("brok->", msg)
if err != nil {
mqcon.Close()
return
}
wbuffer := new(bytes.Buffer)
msg.Encode(wbuffer)
bws.Write(wbuffer.Bytes())
bws.Flush()
wbuffer.Truncate(wbuffer.Len())
}
}()
for {
msg, err := mqtt.DecodeOneMessage(bws, nil)
// fmt.Println("webs->", msg)
if err != nil {
ws.Close()
return
}
msg.Encode(bmqcon)
bmqcon.Flush()
}
}
示例4: Processor
func Processor(ws *websocket.Conn) {
defer ws.Close()
var head Head
const headSize = unsafe.Sizeof(head)
start_time = getNowMillisecond()
ws.PayloadType = 2
file, err := os.Open("record.dat.2")
if err != nil {
log.Print(err)
return
}
defer file.Close()
for {
size := int64(headSize)
buf := make([]byte, size)
n, err := file.Read(buf)
if err == io.EOF {
log.Print("got EOF in Read head")
return
}
if err != nil {
log.Print(err)
return
}
if n != int(size) {
log.Print("invlid head size.")
return
}
err = binary.Read(bytes.NewBuffer(buf), binary.LittleEndian, &head)
if err != nil {
log.Print(err)
return
}
buf = make([]byte, head.BodyLength)
n, err = file.Read(buf)
if err == io.EOF {
log.Print("got EOF in Read body")
return
}
if err != nil {
log.Print(err)
return
}
if n != int(head.BodyLength) {
log.Print("invlid body size.")
return
}
// ignore packet which send from ws client
if head.Type == 1 {
continue
}
now := getNowMillisecond()
toffset := now - start_time
var delay uint32
if head.TimeDelta <= uint32(toffset) {
delay = 1
} else {
delay = (head.TimeDelta) - uint32(toffset)
}
sleep := time.Duration(int(delay)) * time.Millisecond
time.Sleep(sleep)
n, err = ws.Write(buf)
if err == syscall.EPIPE {
log.Print("Websocket Got Broken PIPE")
return
} else if err != nil {
log.Print("Websocket Write Failed")
log.Print(err)
return
}
if n != int(head.BodyLength) {
log.Print("Send body failed")
return
}
}
}
示例5: handleWebsocketConn
func handleWebsocketConn(ws *websocket.Conn) {
ws.PayloadType = websocket.BinaryFrame
s := gameserver.NewSession(dispatcher.GetNextSessionId(), gameserver.RawPacketSerializer{}, ws, dispatcher.GetReceiver())
lobby.AddSession(s)
s.Run()
}
示例6: Processor
// 處理WebSocket客戶端的VNC重新播放
func Processor(ws *websocket.Conn) {
var start_time int64
var mutex sync.Mutex
log.Println("Playback Server: New WebSocket Client: ", ws.Request().RemoteAddr)
// 發送定時器
var timer *time.Timer
// 暫停的標誌
var pause bool = false
// 接收恢複信號的同步channel
resume_chan := make(chan bool)
// 暫停時間的總和
var elapsed int64
// 設置WebSocket內容的編碼類型為Binary
ws.PayloadType = 2
notify_chan := make(chan bool)
go WaitSignal(ws, notify_chan, &timer, &pause, resume_chan, &elapsed, &mutex)
var filename string
ws_query := ws.Request().URL.Query()
if value, ok := ws_query["filename"]; ok {
filename = value[0]
} else {
log.Println("Playback Server: Emptry filename in Query")
return
}
defer ws.Close()
// 設置開始時間
start_time = getNowMillisecond()
// 判斷文件路徑是否正確
pwd, _ := os.Getwd()
fullpath := path.Join(pwd, "data", filename)
fullpath = path.Clean(fullpath)
fullpath_dir := path.Dir(fullpath)
correct_dir := path.Join(pwd, "data")
if fullpath_dir != correct_dir {
log.Println("Playback Server: Bad File Path To Open: ", fullpath)
return
}
// 打開VNC記錄文件
file, err := os.OpenFile(fullpath, os.O_RDONLY, 0664)
if err != nil {
log.Println("Playback Server: Failed to open VNC Data file: ", err)
return
}
defer file.Close()
err = syscall.Flock(int(file.Fd()), syscall.LOCK_SH|syscall.LOCK_NB)
if err != nil {
log.Printf("Playback Server: Set %s Shared Lock Failed: %s\n", file.Name(), err)
return
}
// 循環發送VNC記錄文件,直到文件結束
for {
select {
// 從channel中讀取信號,並退出函數
case quit := <-notify_chan:
if quit == true {
log.Println("Playback Server: Receive Close Signal, Quit Goroutine Now.")
goto end
}
default:
if !pause {
// 獲取Head結構體大小
var head Head
const headSize = unsafe.Sizeof(head)
size := int64(headSize)
buf := make([]byte, size)
n, err := file.Read(buf)
if err == io.EOF {
log.Print("Playback Server: got EOF in Read head")
goto end
}
if err != nil {
log.Print("Playback Server: Failed to Read Head", err)
goto end
}
if n != int(size) {
log.Print("Playback Server: invlid head size.")
goto end
}
err = binary.Read(bytes.NewBuffer(buf), binary.LittleEndian, &head)
if err != nil {
log.Print("Playback Server: Failed to Parse Head: ", err)
goto end
}
//.........這裏部分代碼省略.........