FAQ
WebSockets are bidirectional; you can both read from them and send to them.
In Javascript you send and receive this way:

var ws = new WebSocket("URL")
// send
ws.send("text")
ws.onmessage = function(evt) {
     // receive
     console.log(evt.data)
}


In the echo example you can see that he both sends and receives like this:

func sendRecvServer(ws *websocket.Conn) {
         fmt.Printf("sendRecvServer %#v\n", ws)
         for {
                 var buf string
                 // Receive receives a text message from client, since buf
is string.
                 err := websocket.Message.Receive(ws, &buf)
                 if err != nil {
                         fmt.Println(err)
                         break
                 }
                 fmt.Printf("recv:%q\n", buf)
                 // Send sends a text message to client, since buf is string.
                 err = websocket.Message.Send(ws, buf)
                 if err != nil {
                         fmt.Println(err)
                         break
                 }
                 fmt.Printf("send:%q\n", buf)
         }
         fmt.Println("sendRecvServer finished")
}


So if you hit the /sendRecvText url it should echo back anything you send
it. To make that show up on the page you just need to do something like:
(in your onmessage handler)

document.getElementById("someid").textContent += evt.data;


If that doesn't make sense you may want to read up on Javascript and how it
interacts with the DOM. (You might find a library like jQuery easier to use)

On Tue, Jan 21, 2014 at 5:43 AM, kate Fernando wrote:


I used the code in follow url.


https://github.com/ukai/go-websocket-sample/blob/master/websocket_echo_sample.go

when the user submit the value through browser, I need to output that
value to the webpage.how can I achieve that?

--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 4 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 21, '14 at 12:15p
activeJan 21, '14 at 2:48p
posts4
users2
websitegolang.org

2 users in discussion

kate Fernando: 3 posts Caleb Doxsey: 1 post

People

Translate

site design / logo © 2022 Grokbase