diff --git a/.gitignore b/.gitignore index 18d19d0..c615fa0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor improved-giggle +/old diff --git a/Gopkg.lock b/Gopkg.lock index a667c76..ffb45ea 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -13,9 +13,30 @@ packages = ["."] revision = "8932dfe1d5c8eea12a90372a7fbd0ecf003afb71" +[[projects]] + name = "github.com/sirupsen/logrus" + packages = ["."] + revision = "d682213848ed68c0a260ca37d6dd5ace8423f5ba" + version = "v1.0.4" + +[[projects]] + branch = "master" + name = "golang.org/x/crypto" + packages = ["ssh/terminal"] + revision = "91a49db82a88618983a78a06c1cbd4e00ab749ab" + +[[projects]] + branch = "master" + name = "golang.org/x/sys" + packages = [ + "unix", + "windows" + ] + revision = "dd2ff4accc098aceecb86b36eaa7829b2a17b1c9" + [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "3e660fdcf9fd331fc96db063246816876280939c02d47ae51bf32d39f7280d4f" + inputs-digest = "70923857844a07f13b3c0bd9e900e7b85717299f6b9c6ff36e5b4af2aac02da2" solver-name = "gps-cdcl" solver-version = 1 diff --git a/balance/balance.go b/balance/balance.go new file mode 100644 index 0000000..c6041b8 --- /dev/null +++ b/balance/balance.go @@ -0,0 +1,35 @@ +package balance + +import ( + "math/rand" + "net/http" + "strconv" + + log "github.com/sirupsen/logrus" +) + +func balanceHomeHandler(w http.ResponseWriter, r *http.Request) { + log.Println(r.URL) + if r.URL.Path != "/home" { + http.Error(w, "Not found", http.StatusNotFound) + return + } + if r.Method != "GET" { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + http.ServeFile(w, r, "html/home.html") +} + +//PushBalance sends it back to the client +func PushBalance(userid int, balance int) { + log.Debug("Pushing " + strconv.Itoa(balance) + " to " + strconv.Itoa(userid)) +} + +//GetBalance just returns a random number for now +func GetBalance(userid int) int { + var balance int + balance = rand.Intn(100) + log.Debug("Balance for " + strconv.Itoa(userid) + " = " + strconv.Itoa(balance)) + return balance +} diff --git a/client.go b/client.go new file mode 100644 index 0000000..ad88df8 --- /dev/null +++ b/client.go @@ -0,0 +1,130 @@ +package main + +import ( + "bytes" + "log" + "net/http" + "time" + + "github.com/gorilla/websocket" +) + +const ( + // Time allowed to write a message to the peer. + writeWait = 10 * time.Second + + // Time allowed to read the next pong message from the peer. + pongWait = 60 * time.Second + + // Send pings to peer with this period. Must be less than pongWait. + pingPeriod = (pongWait * 9) / 10 + + // Maximum message size allowed from peer. + maxMessageSize = 512 +) + +/* +var ( + newline = []byte{'\n'} + space = []byte{' '} +) +*/ + +//Client is a middleman between the websocket connection and the hub. +type Client struct { + hub *Hub + + // The websocket connection. + conn *websocket.Conn + + // Buffered channel of outbound messages. + send chan []byte +} + +// readPump pumps messages from the websocket connection to the hub. +// +// The application runs readPump in a per-connection goroutine. The application +// ensures that there is at most one reader on a connection by executing all +// reads from this goroutine. +func (c *Client) readPump() { + defer func() { + c.hub.unregister <- c + c.conn.Close() + }() + c.conn.SetReadLimit(maxMessageSize) + c.conn.SetReadDeadline(time.Now().Add(pongWait)) + c.conn.SetPongHandler(func(string) error { c.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil }) + for { + _, message, err := c.conn.ReadMessage() + if err != nil { + if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { + log.Printf("error: %v", err) + } + break + } + message = bytes.TrimSpace(bytes.Replace(message, newline, space, -1)) + c.hub.broadcast <- message + } +} + +// writePump pumps messages from the hub to the websocket connection. +// +// A goroutine running writePump is started for each connection. The +// application ensures that there is at most one writer to a connection by +// executing all writes from this goroutine. +func (c *Client) writePump() { + ticker := time.NewTicker(pingPeriod) + defer func() { + ticker.Stop() + c.conn.Close() + }() + for { + select { + case message, ok := <-c.send: + c.conn.SetWriteDeadline(time.Now().Add(writeWait)) + if !ok { + // The hub closed the channel. + c.conn.WriteMessage(websocket.CloseMessage, []byte{}) + return + } + + w, err := c.conn.NextWriter(websocket.TextMessage) + if err != nil { + return + } + w.Write(message) + + // Add queued chat messages to the current websocket message. + n := len(c.send) + for i := 0; i < n; i++ { + w.Write(newline) + w.Write(<-c.send) + } + + if err := w.Close(); err != nil { + return + } + case <-ticker.C: + c.conn.SetWriteDeadline(time.Now().Add(writeWait)) + if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil { + return + } + } + } +} + +// serveWs handles websocket requests from the peer. +func serveWs(hub *Hub, w http.ResponseWriter, r *http.Request) { + conn, err := upgrader.Upgrade(w, r, nil) + if err != nil { + log.Println(err) + return + } + client := &Client{hub: hub, conn: conn, send: make(chan []byte, 256)} + client.hub.register <- client + + // Allow collection of memory referenced by the caller by doing all work in + // new goroutines. + go client.writePump() + go client.readPump() +} diff --git a/html/My Infrequent Thoughts.html b/html/My Infrequent Thoughts.html new file mode 100644 index 0000000..83551f0 --- /dev/null +++ b/html/My Infrequent Thoughts.html @@ -0,0 +1,235 @@ + + + + + + + + + + + + + + + + + My Infrequent Thoughts + + + + + + + + + + + + + +
+ + + +
+ + + + + +
+ + +
+
+
+

Recent Posts

+
+ +
    + +
  • +
    + + Posted on + + + +
    +
    +

    + Movinghouse +

    +
    +
  • + + +
  • +
    + + Posted on + + + +
    +
    +

    + GCP Exam +

    +
    +
  • + + +
+
+
+
+ + + +
+
+ + + + + + + \ No newline at end of file diff --git a/html/My Infrequent Thoughts_files/logo.png b/html/My Infrequent Thoughts_files/logo.png new file mode 100644 index 0000000..7d21219 Binary files /dev/null and b/html/My Infrequent Thoughts_files/logo.png differ diff --git a/html/My Infrequent Thoughts_files/main.css b/html/My Infrequent Thoughts_files/main.css new file mode 100644 index 0000000..431697f --- /dev/null +++ b/html/My Infrequent Thoughts_files/main.css @@ -0,0 +1,5 @@ +/*! normalize.css v8.0.0 | MIT License | github.com/necolas/normalize.css */html{line-height:1.15;-webkit-text-size-adjust:100%}body{margin:0}h1{font-size:2em;margin:.67em 0}hr{box-sizing:content-box;height:0;overflow:visible}pre{font-family:monospace,monospace;font-size:1em}a{background-color:transparent}abbr[title]{border-bottom:none;text-decoration:underline;-webkit-text-decoration:underline dotted;text-decoration:underline dotted}b,strong{font-weight:bolder}code,kbd,samp{font-family:monospace,monospace;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}img{border-style:none}button,input,optgroup,select,textarea{font-family:inherit;font-size:100%;line-height:1.15;margin:0}button,input{overflow:visible}button,select{text-transform:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner,button::-moz-focus-inner{border-style:none;padding:0}[type=button]:-moz-focusring,[type=reset]:-moz-focusring,[type=submit]:-moz-focusring,button:-moz-focusring{outline:1px dotted ButtonText}fieldset{padding:.35em .75em .625em}legend{box-sizing:border-box;color:inherit;display:table;max-width:100%;padding:0;white-space:normal}progress{vertical-align:baseline}textarea{overflow:auto}[type=checkbox],[type=radio]{box-sizing:border-box;padding:0}[type=number]::-webkit-inner-spin-button,[type=number]::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}[type=search]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}details{display:block}summary{display:list-item}[hidden],template{display:none} +/*! + * Theme Name: Minimo + * Author: Munif Tanjim + */body{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Open Sans,Helvetica Neue,sans-serif;font-size:1em;line-height:1.5;color:#37474f;word-wrap:break-word}@media screen and (min-width:480px){body{font-size:1.125em}}.title,h1,h2,h3,h4,h5,h6{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Open Sans,Helvetica Neue,sans-serif;font-weight:700;line-height:1.2;color:#263238;margin:0 0 .625em}.comment-content h2,.comment-content h3,.comment-content h4,.comment-content h5,.comment-content h6,.entry-content h2,.entry-content h3,.entry-content h4,.entry-content h5,.entry-content h6{margin-top:1.5em}.comment-content h2:first-child,.comment-content h3:first-child,.comment-content h4:first-child,.comment-content h5:first-child,.comment-content h6:first-child,.entry-content h2:first-child,.entry-content h3:first-child,.entry-content h4:first-child,.entry-content h5:first-child,.entry-content h6:first-child{margin-top:0}.site-title,h1{font-size:2em}h2{font-size:1.75em}h3{font-size:1.5em}h4{font-size:1.375em}h5{font-size:1.25em}h6{font-size:1.125em}p{margin:0 0 1.5em}blockquote,cite,dfn,em,i{font-style:italic}blockquote{font-size:1.125em;padding-left:1em;border-left:.2em solid #bdbdbd}address,blockquote{margin:0 0 1.5em}code,kbd,pre,samp,var{font-family:Menlo,Consolas,Monaco,Ubuntu Mono,Liberation Mono,Lucida Console,monospace;background:#eee;color:#212121;border:1px solid #dcdcdc;border-radius:.25em;padding:.1em .25em}pre{overflow:auto;word-wrap:normal;text-align:initial;margin-bottom:1.5em;padding:.75em 1em}pre code,pre kbd,pre samp,pre var{background:none;color:inherit;border:0;padding:0}code,kbd,samp,var{font-size:.9375em}.highlight pre{border:0;margin:0}.comment-content .highlight,.entry-content .highlight{margin-bottom:1.5em}abbr,acronym{border-bottom:1px dotted #212121;cursor:help}ins,mark{background:#212121;text-decoration:none}big{font-size:125%}.desc{font-style:italic}html{box-sizing:border-box}*,:after,:before{box-sizing:inherit}blockquote,q{quotes:"" ""}blockquote:after,blockquote:before,q:after,q:before{content:""}blockquote :last-child,q :last-child{margin-bottom:0}hr{border:0;height:1px;margin:2em 0;background-color:#bdbdbd}img{max-width:100%;height:auto}figure{margin:1em 0}a{color:#212121;text-decoration:none;transition:color .1s ease-in-out}a:focus{outline-color:#ffcd00;outline-color:var(--color-accent)}a:active,a:hover{outline:0}a:active,a:focus,a:hover{color:#37474f}a.button{display:inline-block}ol,ul{padding:0;margin:0 0 1.5em 2em}ul{list-style:disc}ol{list-style:decimal}li>ol,li>ul{padding-left:1.5em;margin-bottom:0;margin-left:.5em}dt{font-weight:700}dd{margin:0 1.5em 1.5em}table,td,th{border:1px solid #bdbdbd}table{width:100%;margin:0 0 1.5em;table-layout:fixed;border-collapse:separate;border-spacing:0;border-width:1px 0 0 1px}caption,td,th{font-weight:400;text-align:left}th{font-weight:700}td,th{border-width:0 1px 1px 0}td,th{padding:.375em}.button,button,input[type=button],input[type=reset],input[type=submit]{width:auto;padding:.5em .75em;border:1px solid #bdbdbd;border-radius:.2em;background:#fff;color:#37474f;font-size:1em;font-weight:700;line-height:1.15;text-align:center;letter-spacing:1px;white-space:nowrap;text-transform:uppercase;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:pointer;transition:.1s ease-in-out}.button:focus,button:focus,input[type=button]:focus,input[type=reset]:focus,input[type=submit]:focus{outline-color:#ffcd00;outline-color:var(--color-accent)}.button:active,.button:hover,button:active,button:hover,input[type=button]:active,input[type=button]:hover,input[type=reset]:active,input[type=reset]:hover,input[type=submit]:active,input[type=submit]:hover{outline:0}.button:active,.button:focus,.button:hover,button:active,button:focus,button:hover,input[type=button]:active,input[type=button]:focus,input[type=button]:hover,input[type=reset]:active,input[type=reset]:focus,input[type=reset]:hover,input[type=submit]:active,input[type=submit]:focus,input[type=submit]:hover{background:#37474f;color:#fff}input,select,textarea{width:100%;padding:.625em .875em;border:1px solid #bdbdbd;border-radius:.2em;color:#37474f;transition:.1s ease-in-out}input:focus,select:focus,textarea:focus{border-color:#ffcd00;outline-color:transparent}label{font-weight:700;letter-spacing:1px}.menu ul{list-style:none;margin:0}.menu li{text-transform:uppercase}.menu li.current>a{font-weight:700;pointer-events:none}.main-menu>div{display:flex;align-items:center}.main-menu li{display:inline-block;margin-right:.625em}.main-menu li:last-child{margin-right:0}.sidebar-menu .item{display:flex;flex-flow:row wrap;justify-content:flex-end}.sidebar-menu .has-current>a{font-style:italic}.sidebar-menu .sub-menu{display:none;padding-left:0;flex:100%;flex-shrink:0}.sidebar-menu .sub-menu .item:not(.has-children){padding-right:1.2em}.sub-menu-toggler{border:0;padding:0;margin-left:.2em;font-weight:400;width:1em}.sub-menu-toggler:active,.sub-menu-toggler:focus,.sub-menu-toggler:hover{background:none}.sub-menu-toggler>.sign{position:relative;top:-2px}.sub-menu-toggler>.sign:after{content:"+"}.toggled>.sub-menu{display:block}.toggled>.sub-menu-toggler>.sign:after{content:"\2013"}.menu a,.sidebar-toggler span,.sub-menu-toggler,.widget-social_menu a{color:#455a64}.menu a:focus,.menu a:hover,.sidebar-toggler span:focus,.sidebar-toggler span:hover,.sub-menu-toggler:focus,.sub-menu-toggler:hover,.widget-social_menu a:focus,.widget-social_menu a:hover{color:#263238}.entry-nav .sep-before:nth-child(2):before{width:2em;margin:1em 0}.entry-nav div a{display:block;font-style:italic}.entry-nav div span{display:block;font-style:normal;font-size:.875em}.page-links{margin-top:2em}.page-link{display:inline-block;padding:.25em .625em}.page-link:first-child{padding-left:0}.page-link:last-child{padding-right:0}.page-link.current{font-weight:700}.page-link.dots{padding:0}.site{overflow-x:hidden}.main{width:100%;padding-top:2em;transition:margin-left .2s}.sidebar.toggled~.main{margin-left:300px}@media screen and (min-width:920px){.main{width:auto}.has-sidebar .main{float:left;margin-left:300px;padding-left:2em}}.container{width:100%;max-width:740px;padding:0 20px;margin:0 auto}.has-sidebar .container{margin-left:0}.sep-after:after,.sep-before:before{content:"";display:block;width:4em;margin-top:2.5em;margin-bottom:2.5em;border-bottom:.125em solid #bdbdbd}.sidebar{visibility:hidden;float:left;left:-300px;position:absolute;width:300px;max-width:100%;min-height:100%;padding:2em 0;margin-right:-100%;outline:0;z-index:99999;transition:left .2s;text-align:right;background:#fff}.sidebar.toggled{visibility:visible;left:0}.sidebar .sep-after:after{margin-left:auto}@media screen and (min-width:920px){.sidebar{visibility:visible;left:0}}.sidebar-toggler{background:none;border:0}.sidebar-toggler .icon{font-size:1.25em}.sidebar-toggler:active,.sidebar-toggler:focus,.sidebar-toggler:hover{background:none}.main .sidebar-toggler{padding:0;position:absolute;right:20px}.main .sidebar-toggler .close{display:none}.main-menu .sidebar-toggler{position:static;margin-right:.75em}.sidebar .sidebar-toggler{position:absolute;top:.5em;left:.5em;z-index:2027}.sidebar .sidebar-toggler .open{display:none}@media screen and (min-width:920px){.sidebar-toggler{display:none}}.sidebar-overlay{display:none}.sidebar.toggled .sidebar-overlay{display:block}.sidebar.toggled .sidebar-overlay:after{content:"";display:block;position:fixed;top:0;right:0;bottom:0;left:0;z-index:-2027;background:#fff;opacity:.75}@media screen and (min-width:920px){.sidebar.toggled .sidebar-overlay{display:none}}.header .sep-after:after{border-bottom-color:#ffcd00;border-bottom-color:var(--color-accent)}.has-cover .header .sep-after:after,.site-header{display:none}.error404 .site-header,.home .site-header{display:flex}.header-info{margin-top:1.5em}.header-info .title{margin-bottom:0}.header-info .desc{margin:.5em 0 0}.header-info .taxonomy-name{font-size:.875em;margin-right:.25em;font-style:italic}.entry-meta{margin-top:.75em}.entry-meta span{font-size:.9375em;margin-right:.75em;font-style:italic}.entry-meta .icon{margin-right:.25em}.entry-cover figure{margin-top:2em;margin-bottom:2.5em}.entry-cover img{display:block}.entry-cover figcaption{text-align:center;margin:.25em auto 0;padding:0}.entry-cover figcaption span{font-size:.875em}.cover-wide{padding:0;max-width:1080px}.cover-full{padding:0;max-width:100%}.cover-full img{width:100%}details,summary{display:block}summary{cursor:pointer}.no-details details:not([open])>:not(summary){display:none}.no-details details>summary:before{content:"\25BC";display:inline-block;margin-right:.25em;transition:.1s;-webkit-transform:rotate(-90deg);transform:rotate(-90deg)}.no-details details[open]>summary:before{-webkit-transform:rotate(0deg);transform:rotate(0deg)}.entry-toc{margin-bottom:2.5em}.entry-toc ul{list-style:none;margin:0}.comment-content>:last-child,.comment-content>:last-child>:last-child,.entry-content>:last-child,.entry-content>:last-child>:last-child{margin-bottom:0}.comment-content a,.entry-content a{border-bottom:.125em dashed #bdbdbd}.comment-content a:focus,.comment-content a:hover,.entry-content a:focus,.entry-content a:hover{border-bottom-color:#ffcd00;border-bottom-color:var(--color-accent)}.comment-content a.footnote-return,.entry-content a.footnote-return{border-bottom:none}.entry-footer .container>div{margin-top:.5em}.entry-footer .container>div:first-child{margin-top:2.5em}.entry-footer .taxonomy-icon{margin-right:.375em}.comments ol{list-style:none;margin:0}.comments-title{font-size:1.25em;margin-bottom:1.5em}.comment{position:relative;margin:1em 0}.comment-header{min-height:3em}.comment .avatar{float:left;height:3em;width:3em;border-radius:.25em;margin-right:.75em}.comment-meta{font-size:.875em}.comment .fn{display:inline-block;margin-top:.1em}.comment .reply{position:absolute;right:0;margin-top:-2em}.comment .reply,.comment .reply a{display:inline-block}.comment-content{margin-top:.75em;border-radius:.25em;border:1px solid #bdbdbd;padding:1em;padding-bottom:2em}.comment-respond{margin:1.5em 0}.comment-reply-title small{display:inline-block;font-size:.5em;margin-left:1em}.comment-form label{display:inline-block;margin-bottom:.25em}.comment-form>div{margin-top:1em}.comment-submission-feedback{display:none;position:relative;border-radius:.25em;border:1px solid #bdbdbd;padding:1.5em;margin-bottom:1.5em}.comment-submission-feedback a{position:absolute;top:1.5em;right:1.5em;font-size:.875em}.comment-submission-feedback:target{display:block}.list{margin:0 auto;list-style:none}.entry+.list-container{margin-top:2.5em}.list .item{display:flex;width:100%;margin-top:1em}.list .item:first-of-type{margin-top:0}.list .item .meta{flex:0 0 6em}.list .item .meta span{font-size:.875em}.list .item-header{flex:1}.list .item-title{font-size:1em;font-weight:400;margin-bottom:0;line-height:inherit}.list .item .icon{margin-right:.25em}.taxonomy-cloud{display:flex;flex-wrap:wrap;align-items:center}.taxonomy-cloud li{max-width:100%;padding:0 1.5em .5em 0;text-overflow:ellipsis;white-space:nowrap;overflow:hidden}.footer .sep-before:before{border-bottom-color:#ffcd00;border-bottom-color:var(--color-accent)}.footer a{font-style:italic}.footer .container>div,.footer .container>section{width:100%;margin-top:1em}.footer .container>div:first-child,.footer .container>section:first-child{margin-top:0}.footer .container>div:last-child,.footer .container>section:last-child{margin-bottom:2em}.error404 .gopher{margin:0}.error404 .gopher img{width:17.5em}.error404 .gopher a{display:block;border:0}.error404 .gopher figcaption{margin-top:1em}.error404 .gopher .title{margin-bottom:0}.home-widgets .sep-before:before{display:none}.entry+.home-widgets .sep-before:before{display:block}.icon{display:inline-block;stroke:currentColor;fill:none;position:relative;top:2px}.icon,img.emoji{height:1em;width:1em}img.emoji{margin:0 .05em 0 .1em;vertical-align:-.1em}.layout-archive .list .item{padding-left:1em}.layout-archive .list .item .meta{flex:0 0 3.5em}.archive-group.sep-after:last-child:after{display:none}.widget-title{margin-bottom:1em}.widget.sep-after:after{margin-top:1.5em;margin-bottom:1.5em}.widget.sep-after:last-of-type:after{display:none}.sidebar .widget-title{margin-bottom:.5em;font-size:1.25em}.sidebar .widget .container{padding:0}.widget-about .logo{max-width:5em}.widget-about .logo a{display:block;line-height:0}.sidebar .widget-about .logo{margin-left:auto}.widget-about .site-title{margin-bottom:0;font-size:1.75em}.sidebar .widget-about.sep-after:after{border-bottom-color:#ffcd00;border-bottom-color:var(--color-accent)}.sidebar .widget-recent_posts .meta{display:none}.sidebar .widget-recent_posts .item{margin-top:0}.widget-social_menu ul{list-style:none;padding:0;margin:0}.widget-social_menu li{display:inline-block;margin-right:.625em}.widget-social_menu li:last-child{margin-right:0}.widget-social_menu li a{font-size:1.125em}.sidebar .widget-social_menu li{margin-right:0;margin-left:.625em}.sidebar .widget-taxonomy_cloud .taxonomy-cloud{justify-content:flex-end;font-size:.875em}.sidebar .widget-taxonomy_cloud li{padding:0 0 .4em .8em}.comment .says,.screen-reader-text{position:absolute;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;width:1px;margin:-1px;padding:0;border:0;overflow:hidden;word-wrap:normal!important}.comment .says:focus,.screen-reader-text:focus{display:block;left:.5em;top:.5em;clip:auto;-webkit-clip-path:none;clip-path:none;height:auto;width:auto;padding:1em;border-radius:.25em;font-size:.875em;font-weight:700;line-height:normal;text-decoration:none;background:#212121;color:#fafafa;z-index:100000}.align-center{text-align:center}.align-center>blockquote{display:inline-block}.align-center>ol,.align-center>ul{display:inline-block;text-align:initial}.align-center li{word-wrap:normal}.convo ol{display:table;list-style:none;margin:0}.convo li{display:table-row}.convo li>div{display:table-cell;padding-bottom:1.5em}.convo .person{font-size:.9em;font-style:italic;text-align:right}.convo .sep{padding-left:.75em;padding-right:.5em}.text-shortcode *{color:inherit;font-size:1em} diff --git a/html/My Infrequent Thoughts_files/main.js b/html/My Infrequent Thoughts_files/main.js new file mode 100644 index 0000000..9136269 --- /dev/null +++ b/html/My Infrequent Thoughts_files/main.js @@ -0,0 +1 @@ +!function(e){function t(t){for(var n,r,u=t[0],i=t[1],c=0,l=[];cb?p>w?y?(y=!1,h=A>0?A:0,n.setAttribute("style","top: "+h+"px;")):!g&&p+b>v+A&&v0?A:0,n.setAttribute("style","top: "+h+"px;")):!y&&p + + +Chat Example + + + + +
+
+ + +
+ + diff --git a/hub.go b/hub.go new file mode 100644 index 0000000..7727c6c --- /dev/null +++ b/hub.go @@ -0,0 +1,52 @@ +// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +//Hub maintains the set of active clients and broadcasts messages to the clients. +type Hub struct { + // Registered clients. + clients map[*Client]bool + + // Inbound messages from the clients. + broadcast chan []byte + + // Register requests from the clients. + register chan *Client + + // Unregister requests from clients. + unregister chan *Client +} + +func newHub() *Hub { + return &Hub{ + broadcast: make(chan []byte), + register: make(chan *Client), + unregister: make(chan *Client), + clients: make(map[*Client]bool), + } +} + +func (h *Hub) run() { + for { + select { + case client := <-h.register: + h.clients[client] = true + case client := <-h.unregister: + if _, ok := h.clients[client]; ok { + delete(h.clients, client) + close(client.send) + } + case message := <-h.broadcast: + for client := range h.clients { + select { + case client.send <- message: + default: + close(client.send) + delete(h.clients, client) + } + } + } + } +} diff --git a/main.go b/main.go index 349689b..0ce5d48 100644 --- a/main.go +++ b/main.go @@ -1,18 +1,15 @@ package main import ( + "bytes" "flag" - "fmt" - "html/template" - "math/rand" "net/http" "os" - "strconv" "time" "github.com/gorilla/websocket" - "github.com/sgoodliff/hello" + "github.com/sgoodliff/improved-giggle/balance" log "github.com/sirupsen/logrus" ) @@ -28,11 +25,16 @@ func init() { log.SetLevel(log.DebugLevel) } -var addr = flag.String("addr", "localhost:8080", "http service address") +var ( + newline = []byte{'\n'} + space = []byte{' '} -var upgrader = websocket.Upgrader{} // use default options + addr = flag.String("addr", "localhost:8080", "http service address") + upgrader = websocket.Upgrader{} // use default options +) -func echo(w http.ResponseWriter, r *http.Request) { +func balanceHandler(w http.ResponseWriter, r *http.Request) { + b := []byte("Hello, goodbye, etc!") c, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Print("upgrade:", err) @@ -40,164 +42,44 @@ func echo(w http.ResponseWriter, r *http.Request) { } defer c.Close() for { - mt, message, err := c.ReadMessage() - if err != nil { - log.Println("read:", err) - break - } - log.Printf("recv: %s", message) - err = c.WriteMessage(mt, message) - if err != nil { - log.Println("write:", err) - break - } - } -} -func home(w http.ResponseWriter, r *http.Request) { - homeTemplate.Execute(w, "ws://"+r.Host+"/echo") -} -func balance(w http.ResponseWriter, r *http.Request) { - c, err := upgrader.Upgrade(w, r, nil) - if err != nil { - log.Print("upgrade:", err) - return - } - defer c.Close() - for { - mt, message, err := c.ReadMessage() - if err != nil { - log.Println("read:", err) - break - } - log.Printf("recv: %s", message) - err = c.WriteMessage(mt, message) + b = bytes.TrimSpace(bytes.Replace(b, newline, space, -1)) + log.Printf("recv: %s", b) + err = c.WriteMessage(websocket.TextMessage, b) if err != nil { log.Println("write:", err) break } + time.Sleep(1000 * time.Millisecond) + } } -func pushBalance(userid int,balance int) { - log.Debug("Pushing " + strconv.Itoa(balance) + " to " + strconv.Itoa(userid) ) -} -func getBalance(userid int) int { - var balance int - balance = rand.Intn(100) - log.Debug("Balance for " + strconv.Itoa(userid) + " = " + strconv.Itoa(balance)) - return balance -} - func updateData() { balances := make(map[int]int) - var balance int + var mybalance int for { for i := 0; i < 100; i++ { - balance = getBalance(i) - if balance != balances[i] { + mybalance = balance.GetBalance(i) + if mybalance != balances[i] { log.Debug("Current balance has changed") - balances[i] = balance + balances[i] = mybalance // we need to push this to client - pushBalance(i,balance) + balance.PushBalance(i, mybalance) } } - time.Sleep(300 * time.Millisecond) + time.Sleep(10000 * time.Millisecond) } } func main() { - + hub := newHub() + go hub.run() log.Info("Hello, World") - var x string - x = hello.Hello() - fmt.Printf(x) - - flag.Parse() - - go updateData() - - http.HandleFunc("/echo", echo) - http.HandleFunc("/", home) - http.HandleFunc("/balance", balance) + //go updateData() + http.HandleFunc("/home", balanceHomeHandler) + // http.HandleFunc("/ws", balanceHandler) + http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { + serveWs(hub, w, r) + }) log.Fatal(http.ListenAndServe(*addr, nil)) - } - -var homeTemplate = template.Must(template.New("").Parse(` - - - - - - - - -
-

Click "Open" to create a connection to the server, -"Send" to send a message to the server and "Close" to close the connection. -You can change the message and send multiple times. -

-

- - -

- -

-
-
-
- - -`))