David Symonds | bbcdc9d | 2014-02-19 11:36:36 +1100 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All rights reserved. |
| 2 | // Use of this source code is governed by the Apache 2.0 |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | /* |
| 6 | Package channel implements the server side of App Engine's Channel API. |
| 7 | |
| 8 | Create creates a new channel associated with the given clientID, |
| 9 | which must be unique to the client that will use the returned token. |
| 10 | |
| 11 | token, err := channel.Create(c, "player1") |
| 12 | if err != nil { |
| 13 | // handle error |
| 14 | } |
| 15 | // return token to the client in an HTTP response |
| 16 | |
| 17 | Send sends a message to the client over the channel identified by clientID. |
| 18 | |
| 19 | channel.Send(c, "player1", "Game over!") |
| 20 | */ |
Dave Day | d510d6e | 2014-11-13 17:12:34 +1100 | [diff] [blame^] | 21 | package channel // import "google.golang.org/appengine/channel" |
David Symonds | bbcdc9d | 2014-02-19 11:36:36 +1100 | [diff] [blame] | 22 | |
| 23 | import ( |
| 24 | "encoding/json" |
| 25 | |
| 26 | "google.golang.org/appengine" |
| 27 | "google.golang.org/appengine/internal" |
| 28 | basepb "google.golang.org/appengine/internal/base" |
| 29 | pb "google.golang.org/appengine/internal/channel" |
| 30 | ) |
| 31 | |
| 32 | // Create creates a channel and returns a token for use by the client. |
| 33 | // The clientID is an application-provided string used to identify the client. |
| 34 | func Create(c appengine.Context, clientID string) (token string, err error) { |
| 35 | req := &pb.CreateChannelRequest{ |
| 36 | ApplicationKey: &clientID, |
| 37 | } |
| 38 | resp := &pb.CreateChannelResponse{} |
| 39 | err = c.Call(service, "CreateChannel", req, resp, nil) |
| 40 | token = resp.GetToken() |
| 41 | return token, remapError(err) |
| 42 | } |
| 43 | |
| 44 | // Send sends a message on the channel associated with clientID. |
| 45 | func Send(c appengine.Context, clientID, message string) error { |
| 46 | req := &pb.SendMessageRequest{ |
| 47 | ApplicationKey: &clientID, |
| 48 | Message: &message, |
| 49 | } |
| 50 | resp := &basepb.VoidProto{} |
| 51 | return remapError(c.Call(service, "SendChannelMessage", req, resp, nil)) |
| 52 | } |
| 53 | |
| 54 | // SendJSON is a helper function that sends a JSON-encoded value |
| 55 | // on the channel associated with clientID. |
| 56 | func SendJSON(c appengine.Context, clientID string, value interface{}) error { |
| 57 | m, err := json.Marshal(value) |
| 58 | if err != nil { |
| 59 | return err |
| 60 | } |
| 61 | return Send(c, clientID, string(m)) |
| 62 | } |
| 63 | |
| 64 | // remapError fixes any APIError referencing "xmpp" into one referencing "channel". |
| 65 | func remapError(err error) error { |
| 66 | if e, ok := err.(*internal.APIError); ok { |
| 67 | if e.Service == "xmpp" { |
| 68 | e.Service = "channel" |
| 69 | } |
| 70 | } |
| 71 | return err |
| 72 | } |
| 73 | |
| 74 | var service = "xmpp" // prod |
| 75 | |
| 76 | func init() { |
| 77 | if appengine.IsDevAppServer() { |
| 78 | service = "channel" // dev |
| 79 | } |
| 80 | internal.RegisterErrorCodeMap("channel", pb.ChannelServiceError_ErrorCode_name) |
| 81 | } |