Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
ws conn works; no chat output
  • Loading branch information
Oskar Thoren committed Aug 9, 2012
1 parent 63954fb commit d3d2055
Show file tree
Hide file tree
Showing 800 changed files with 250,742 additions and 26 deletions.
Binary file added .main.go.swp
Binary file not shown.
64 changes: 64 additions & 0 deletions SPEC
@@ -0,0 +1,64 @@
HEAVILY INFLUENCED / COPY-PASTED FROM
http://gary.beagledreams.com/page/go-websocket-chat.html

Use websockets to send and receive messages between peers and a server hub.
We have a bunch of connections, and a hub.

- hook up to the hub (add to a list of connections)
- hook down from the hub (rm ~)
- send json shit to hub
- receive json shit from hub (which got it from other connections)

what do we want to do. i am lost i think cause i donr see how we are supposed to TEST this

like we got this websocket example, but if we change it how do we know what did makes sense?

we can change the html as we go too. yeah ok.

# Hub
connections map[*connection]bool
broadcast chan string
register chan *connection
unregister chan *connection

# Connection
ws *websocket.Conn
send chan string

# What client sends to server
{ "event": "event name", "data": "ur data man" }

# NodeJS
conn = new WebSocket(network); // network some url
conn.send(data);
conn.onmessage = function(data) {};
// and 2 or 3 more events like onmessage

map these go concepts into either 1) plain english 2) other lingo you use

One connection per websocket, and each connection has what channels?
Ie: what different type of data does it process? We want to tell it to:

so thats just what is in the send channel? json {"event_name": "foo"}? ok those 4 are fine, how do we spec them nicely - thats channels right, and 2 types so far connection and hub

so we need propertieis of both and list them nicely (sort of what we have already)

# NODEJS
var rtc = {};
rtc._events = {};
var rooms = {};

just writing down so we can make analogous things in go.
this is the server?

ohkay, they changed it a bit

## EVENTS

.on
.fire wtf is that

## ROUTES
serve /webrtc.io/webrtc.io.js (the client)

## ROOMS
56 changes: 48 additions & 8 deletions conn.go
@@ -1,15 +1,19 @@
package main package main


import ( import (
"fmt"
"encoding/json"
"code.google.com/p/go.net/websocket" "code.google.com/p/go.net/websocket"
) )


type connection struct { type connection struct {
// The websocket connection.
ws *websocket.Conn ws *websocket.Conn
send chan Event
}


// Buffered channel of outbound messages. type Event struct {
send chan string Name string `json:"eventName"`
Data interface{} `json:"data"` // takes arbitrary datas
} }


func (c *connection) reader() { func (c *connection) reader() {
Expand All @@ -19,23 +23,59 @@ func (c *connection) reader() {
if err != nil { if err != nil {
break break
} }
h.broadcast <- message
var ev Event
if err := json.Unmarshal([]byte(message), &ev); err != nil {
fmt.Println(err)
break
}
h.broadcast <- ev
} }
c.ws.Close() c.ws.Close()
} }


func (c *connection) writer() { func (c *connection) writer() {
for message := range c.send { for ev := range c.send {
err := websocket.Message.Send(c.ws, message) // server should add color to each chat message
if err != nil { // we want this: {msg: "im saying something", color: "#aasdi"}

msg, err := json.Marshal(ev)
if err != nil {
break
}

//check ev.Name, if it's == 'chat msg'... this becomes a real handler.
// so if it is not, just send it, but if it is
// err wait. this feels a bit backwards no?
// like why are we adjusting the server to the client?
// should be the other way around?
// if chat msg is arbitrary that is
// how do u send the color to the client?
// i guess, but that's... application specific?

// yeah it's app specific... is WWWWWWWWWWhat i WWWWWWas talking about
// with the event handlers stuff
// need a way to register event handlers and parse the data and shit.
// bleh....................... BLEHHHHHHHHHHHH BLUH
// blub blug.
// this would be so ez in js
// LOL
// stfu
// js nerd. "register event handdlers and parse the data and shit"
// string ops in go?
// how done in js btw?
if string(msg) == 'chat msg' {
msg := string(msg // ...
if err := websocket.Message.Send(c.ws, string(msg)); err != nil {
break break
} }
} }
c.ws.Close() c.ws.Close()
} }


func wsHandler(ws *websocket.Conn) { func wsHandler(ws *websocket.Conn) {
c := &connection{send: make(chan string, 256), ws: ws} fmt.Print("New websocket connection ", ws)
c := &connection{send: make(chan Event, 256), ws: ws}
h.register <- c h.register <- c
defer func() { h.unregister <- c }() defer func() { h.unregister <- c }()
go c.writer() go c.writer()
Expand Down

0 comments on commit d3d2055

Please sign in to comment.