mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 19:00:54 +00:00
update chat example
This commit is contained in:
parent
0e278ae358
commit
7075ad8a28
@ -60,9 +60,9 @@ func (c *connection) readPump() {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
switch op {
|
switch op {
|
||||||
case websocket.OpPong:
|
case websocket.PongMessage:
|
||||||
c.ws.SetReadDeadline(time.Now().Add(readWait))
|
c.ws.SetReadDeadline(time.Now().Add(readWait))
|
||||||
case websocket.OpText:
|
case websocket.TextMessage:
|
||||||
message, err := ioutil.ReadAll(r)
|
message, err := ioutil.ReadAll(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
@ -89,14 +89,14 @@ func (c *connection) writePump() {
|
|||||||
select {
|
select {
|
||||||
case message, ok := <-c.send:
|
case message, ok := <-c.send:
|
||||||
if !ok {
|
if !ok {
|
||||||
c.write(websocket.OpClose, []byte{})
|
c.write(websocket.CloseMessage, []byte{})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := c.write(websocket.OpText, message); err != nil {
|
if err := c.write(websocket.TextMessage, message); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
if err := c.write(websocket.OpPing, []byte{}); err != nil {
|
if err := c.write(websocket.PingMessage, []byte{}); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -149,8 +149,13 @@ type WSController struct {
|
|||||||
beego.Controller
|
beego.Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var upgrader = websocket.Upgrader{
|
||||||
|
ReadBufferSize: 1024,
|
||||||
|
WriteBufferSize: 1024,
|
||||||
|
}
|
||||||
|
|
||||||
func (this *WSController) Get() {
|
func (this *WSController) Get() {
|
||||||
ws, err := websocket.Upgrade(this.Ctx.ResponseWriter, this.Ctx.Request.Header, nil, 1024, 1024)
|
ws, err := upgrader.Upgrade(this.Ctx.ResponseWriter, this.Ctx.Request,nil)
|
||||||
if _, ok := err.(websocket.HandshakeError); ok {
|
if _, ok := err.(websocket.HandshakeError); ok {
|
||||||
http.Error(this.Ctx.ResponseWriter, "Not a websocket handshake", 400)
|
http.Error(this.Ctx.ResponseWriter, "Not a websocket handshake", 400)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user