Skip to content

Instantly share code, notes, and snippets.

@michaldarda
Created March 10, 2015 22:47
Show Gist options
  • Save michaldarda/301322512b1a873d026c to your computer and use it in GitHub Desktop.
Save michaldarda/301322512b1a873d026c to your computer and use it in GitHub Desktop.
Pushserver + Pushclient
package main
import "golang.org/x/net/websocket"
import "log"
import "time"
import "fmt"
type Message struct {
Message string `json:"message"`
CreatedAt string `json:"created_at"`
}
func main() {
origin := "http://localhost/"
url := "ws://localhost:8080/"
ws, err := websocket.Dial(url, "", origin)
if err != nil {
log.Fatal(err)
}
var msg Message
for {
if err = websocket.JSON.Receive(ws, &msg); err != nil {
log.Fatal(err)
}
now := time.Now()
createdAt, _ := time.Parse("2006-01-02 15:04:05 UTC", msg.CreatedAt)
fmt.Print("Request took ")
fmt.Println(now.Sub(createdAt))
log.Printf("%+v\n", msg)
}
}
require 'em-websocket'
require 'json'
EM.run {
EM::WebSocket.run(:host => "0.0.0.0", :port => 8080, :debug => true) do |ws|
ws.onopen { |handshake|
puts "WebSocket opened #{{
:path => handshake.path,
:query => handshake.query,
:origin => handshake.origin,
}}"
ws.send({message: "Hello Client!", created_at: Time.now.utc.to_s}.to_json)
}
ws.onmessage { |msg|
ws.send({message: "Hello Client!", created_at: Time.now.utc.to_s}.to_json)
}
ws.onclose {
puts "WebSocket closed"
}
ws.onerror { |e|
ws.send({message: "Error occurred!", created_at: Time.now.utc.to_s}.to_json)
}
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment