Skip to content

Instantly share code, notes, and snippets.

View dpogorzelski's full-sized avatar

Dawid Pogorzelski dpogorzelski

View GitHub Profile
func main() {
r := mux.NewRouter()
r.HandleFunc("/ws", remoteHandler)
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./public/")))
http.ListenAndServe(":3000", r)
}
func remoteHandler(res http.ResponseWriter, req *http.Request) {
var err error
//when someone requires a ws connection we create a new player and store a
// pointer to the connection inside player.Socket
ws, _ := websocket.Upgrade(res, req, nil, 1024, 1024)
log.Printf("got websocket conn from %v\n", ws.RemoteAddr())
player := new(Player)
player.Id = uuid.New()
player.Socket = ws
func (p *Player) position(new bool) Message {
return Message{X: p.X, Y: p.Y, Id: p.Id, New: new, Online: true}
}
var Players = make([]*Player, 0)
type Message struct {
Y int // current Y position
X int // as above
Id string // the id of the player that sent the message
New bool // true if this player just connected so we know when to
// spawn a new sprite on the screens of the other players. for all subsequent
// messages it's false
Online bool // true if the player is no longer connected so the frontend
// will remove it's sprite
}
package main
import (
"code.google.com/p/go-uuid/uuid"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"log"
"net/http"
)
{
"_id" : ObjectId("52fa44d6a0dfdb9c210254d4"),
"created" : ISODate("2014-02-11T15:42:14.953Z"),
"res" : {
"headers" : {
"content-length" : "22",
"content-type" : "text/html; charset=utf-8",
"x-powered-by" : "Express"
},
"body" : "Wrong user or password",
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
SetEnvIf Origin "http(s)?://(domain\.com|foo\.org)$" AccessControlAllowOrigin=$0
Header set Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
Header set Access-Control-Allow-Methods POST,GET,DELETE,PUT,OPTIONS
Header set Access-Control-Allow-Headers X-Requested-With,AUTH_TOKEN,Content-Type
match '/signin_foo' => 'sessions#new_foo', :as => :signin_foo
match '/signin_bar' => 'sessions#new_bar', :as => :signin_bar
def new_foo
redirect_to '/auth/foo/callback'
end
def new_bar
redirect_to '/auth/bar/callback'
end