Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffail committed Jun 9, 2017
1 parent 55a5759 commit ff7d020
Showing 1 changed file with 29 additions and 36 deletions.
65 changes: 29 additions & 36 deletions lib/http/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ import (
"golang.org/x/net/websocket"
)

//--------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------

/*
leapHTTPClientMessage - A structure that defines a message format to expect from clients. Commands
can be 'create' (init with new document), 'edit' (edit existing document) or 'read' (read only
existing document).
*/
// leapHTTPClientMessage - A structure that defines a message format to expect
// from clients. Commands can be 'create' (init with new document), 'edit' (edit
// existing document) or 'read' (read only existing document).
type leapHTTPClientMessage struct {
Command string `json:"command"`
Token string `json:"token"`
Expand All @@ -52,29 +50,27 @@ type leapHTTPClientMessage struct {
Document *store.Document `json:"leap_document,omitempty"`
}

/*
leapHTTPServerMessage - A structure that defines a response message from the server to a client.
Type can be 'document' (init response) or 'error' (an error message to display to the client).
*/
// leapHTTPServerMessage - A structure that defines a response message from the
// server to a client. Type can be 'document' (init response) or 'error' (an
// error message to display to the client).
type leapHTTPServerMessage struct {
Type string `json:"response_type"`
Document store.Document `json:"leap_document,omitempty"`
Version *int `json:"version,omitempty"`
Error string `json:"error,omitempty"`
}

//--------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------

// Common errors for the http package.
var (
ErrInvalidDocument = errors.New("invalid document structure")
ErrInvalidUserID = errors.New("invalid user ID")
)

/*
WebsocketHandler - Returns a websocket handler that routes new websockets to a curator. Use this
with an HTTP server with the "golang.org/x/net/websocket" package.
*/
// WebsocketHandler - Returns a websocket handler that routes new websockets to
// a curator. Use this with an HTTP server with the "golang.org/x/net/websocket"
// package.
func WebsocketHandler(
finder curator.Type,
timeout time.Duration,
Expand Down Expand Up @@ -151,35 +147,32 @@ func WebsocketHandler(
}
}

//--------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------

/*
leapSocketClientMessage - A structure that defines a message format to expect from clients connected
to a text model. Commands can currently be 'submit' (submit a transform to a bound document), or
'update' (submit an update to the users cursor position).
*/
type leapSocketClientMessage struct {
// LeapSocketClientMessage - A structure that defines a message format to expect
// from clients connected to a text model. Commands can currently be 'submit'
// (submit a transform to a bound document), or 'update' (submit an update to
// the users cursor position).
type LeapSocketClientMessage struct {
Command string `json:"command"`
Transform *text.OTransform `json:"transform,omitempty"`
Position *int64 `json:"position,omitempty"`
Message string `json:"message,omitempty"`
}

/*
leapSocketServerMessage - A structure that defines a response message from a text model to a client.
Type can be 'transforms' (continuous delivery), 'correction' (actual version of a submitted
transform), 'update' (an update to a users status) or 'error' (an error message to display to the
client).
*/
type leapSocketServerMessage struct {
// LeapSocketServerMessage - A structure that defines a response message from a
// text model to a client. Type can be 'transforms' (continuous delivery),
// 'correction' (actual version of a submitted transform), 'update' (an update
// to a users status) or 'error' (an error message to display to the client).
type LeapSocketServerMessage struct {
Type string `json:"response_type"`
Transforms []text.OTransform `json:"transforms,omitempty"`
Updates []binder.ClientUpdate `json:"user_updates,omitempty"`
Version int `json:"version,omitempty"`
Error string `json:"error,omitempty"`
}

//--------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------

func serveWebsocketIO(
ws *websocket.Conn,
Expand All @@ -203,7 +196,7 @@ func serveWebsocketIO(
var err error
defer func() {
if err != nil {
websocket.JSON.Send(ws, leapSocketServerMessage{
websocket.JSON.Send(ws, LeapSocketServerMessage{
Type: "error",
Error: err.Error(),
})
Expand All @@ -212,7 +205,7 @@ func serveWebsocketIO(
}()

for atomic.LoadUint32(&incomingCloseInt) == 0 {
var msg leapSocketClientMessage
var msg LeapSocketClientMessage
if socketErr := websocket.JSON.Receive(ws, &msg); socketErr != nil {
return
}
Expand All @@ -226,7 +219,7 @@ func serveWebsocketIO(
if ver, err = portal.SendTransform(*msg.Transform, timeout); err != nil {
return
}
websocket.JSON.Send(ws, leapSocketServerMessage{
websocket.JSON.Send(ws, LeapSocketServerMessage{
Type: "correction",
Version: ver,
})
Expand Down Expand Up @@ -257,15 +250,15 @@ func serveWebsocketIO(
if !open {
return
}
websocket.JSON.Send(ws, leapSocketServerMessage{
websocket.JSON.Send(ws, LeapSocketServerMessage{
Type: "transforms",
Transforms: []text.OTransform{tform},
})
case msg, open := <-portal.UpdateReadChan():
if !open {
return
}
websocket.JSON.Send(ws, leapSocketServerMessage{
websocket.JSON.Send(ws, LeapSocketServerMessage{
Type: "update",
Updates: []binder.ClientUpdate{msg},
})
Expand All @@ -290,4 +283,4 @@ func serveWebsocketIO(
}
}

//--------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------

0 comments on commit ff7d020

Please sign in to comment.