Skip to content

Commit

Permalink
Merge pull request revel#26 from notzippy/server-engine-2
Browse files Browse the repository at this point in the history
Updated chat examples to be compatible with new server engine code
  • Loading branch information
notzippy authored Aug 19, 2017
2 parents 537fcc1 + 7dafda6 commit 7029817
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
26 changes: 18 additions & 8 deletions booking/app/controllers/gorp.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package controllers

import (

"database/sql"
"golang.org/x/crypto/bcrypt"

"golang.org/x/crypto/bcrypt"

"github.com/go-gorp/gorp"
_ "github.com/mattn/go-sqlite3"

r "github.com/revel/revel"

"github.com/revel/modules/db/app"

"github.com/revel/examples/booking/app/models"
r "github.com/revel/revel"

"github.com/revel/examples/booking/app/models"
"time"
)

var (
Expand Down Expand Up @@ -76,6 +76,16 @@ func InitDB() {
panic(err)
}
}
bookings := []*models.Booking{
&models.Booking{0, demoUser.UserId, hotels[0].HotelId, time.Now().Format(models.SQL_DATE_FORMAT), time.Now().Format(models.SQL_DATE_FORMAT), "id1", "n1", 12, 2, false, 2, time.Now(), time.Now(), demoUser, hotels[0]},
&models.Booking{0, demoUser.UserId, hotels[1].HotelId, time.Now().Format(models.SQL_DATE_FORMAT), time.Now().Format(models.SQL_DATE_FORMAT), "id2", "n2", 12, 2, false, 2, time.Now(), time.Now(), demoUser, hotels[1]},
&models.Booking{0, demoUser.UserId, hotels[2].HotelId, time.Now().Format(models.SQL_DATE_FORMAT), time.Now().Format(models.SQL_DATE_FORMAT), "id3", "n3", 12, 2, false, 2, time.Now(), time.Now(), demoUser, hotels[2]},
}
for _, booking := range bookings {
if err := Dbm.Insert(booking); err != nil {
panic(err)
}
}
}

type GorpController struct {
Expand Down
21 changes: 9 additions & 12 deletions chat/app/controllers/websocket.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package controllers

import (

"golang.org/x/net/websocket"

"github.com/revel/revel"

"github.com/revel/examples/chat/app/chatroom"
"github.com/revel/revel"

"github.com/revel/examples/chat/app/chatroom"
)

type WebSocket struct {
Expand All @@ -17,12 +14,12 @@ func (c WebSocket) Room(user string) revel.Result {
return c.Render(user)
}

func (c WebSocket) RoomSocket(user string, ws *websocket.Conn) revel.Result {
func (c WebSocket) RoomSocket(user string, ws revel.ServerWebSocket) revel.Result {
// Make sure the websocket is valid.
if ws == nil {
return nil
return nil
}

// Join the room.
subscription := chatroom.Subscribe()
defer subscription.Cancel()
Expand All @@ -32,7 +29,7 @@ func (c WebSocket) RoomSocket(user string, ws *websocket.Conn) revel.Result {

// Send down the archive.
for _, event := range subscription.Archive {
if websocket.JSON.Send(ws, &event) != nil {
if ws.MessageSendJSON(&event) != nil {
// They disconnected
return nil
}
Expand All @@ -44,7 +41,7 @@ func (c WebSocket) RoomSocket(user string, ws *websocket.Conn) revel.Result {
go func() {
var msg string
for {
err := websocket.Message.Receive(ws, &msg)
err := ws.MessageReceiveJSON(&msg)
if err != nil {
close(newMessages)
return
Expand All @@ -57,7 +54,7 @@ func (c WebSocket) RoomSocket(user string, ws *websocket.Conn) revel.Result {
for {
select {
case event := <-subscription.New:
if websocket.JSON.Send(ws, &event) != nil {
if ws.MessageSendJSON(&event) != nil {
// They disconnected.
return nil
}
Expand Down

0 comments on commit 7029817

Please sign in to comment.