Consider the following sample
import (
"code.google.com/p/go.net/websocket"
"fmt"
"log"
"net/http"
"os"
)
type MessageReceived struct {
Name string
Nbmsg int
}
func Echo(ws *websocket.Conn) {
msg := new(MessageReceived)
for {
websocket.JSON.Receive(ws, &msg)
fmt.Printf("Received message: %+v\n", msg)
if err := websocket.JSON.Send(ws, msg); err != nil {
fmt.Println("Can't send echo")
break
}
//os.Exit(0)
}
}
func checkError(err error) {
if err != nil {
log.Printf("Fatal error: %s\n", err.Error())
os.Exit(1)
}
}
func main() {
http.Handle("/s", websocket.Handler(Echo))
fs := http.FileServer(http.Dir("web"))
err := http.ListenAndServe(":8081",fs)
checkError(err)
}
The page and static files gets served but when but i try to connect to the websocket from javascript i get
WebSocket connection to 'ws://localhost:8081/s' failed: Error during WebSocket handshake: Unexpected response code: 404
The only way i could make it work is by serving static files from a different go application server and serving the websocket also from a separate server application using 2 different ports on the local host, what am i missing in the above sample? how could i serve files and the websocket from the same server?
You're assigning the websocket.Handler to the DefaultServeMux, but only using the FileServer handler in your ListenAndServe call.
Add the FileServer handler to the DefaultServe mux too, with some non-conflicting prefix:
func main() {
http.Handle("/s", websocket.Handler(Echo))
http.Handle("/f", http.FileServer(http.Dir("web"))
err := http.ListenAndServe(":8081", nil)
checkError(err)
}
Related
I'm having a client-server code for http2, I want to send file from client to server. But I'm stuck on how to do that. Means how to break my file in small chucks and send it via that connection.
Any help and link will be appreciated.
Thanks
//http2 client
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"golang.org/x/net/http2"
)
func main() {
client := http.Client{
// InsecureTLSDial is temporary and will likely be
// replaced by a different API later.
Transport: &http2.Transport{InsecureTLSDial: true},
}
// Perform the request
resp, err := client.Post("https://localhost:9191/hello/sayHello", "text/plain", bytes.NewBufferString("Hello Go!"))
if err != nil {
log.Fatalf("Failed get: %s", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Failed reading response body: %s", err)
}
fmt.Printf("Got response %d: %s %s", resp.StatusCode, resp.Proto, string(body))
}
I have the following server/server.go definition for my gRPC server:
package main
import (
"flag"
"github.com/golang/glog"
pb "github.com/go-grpc-tutorial/pb"
"golang.org/x/net/context"
"google.golang.org/grpc"
"net"
)
// Implements of EchoServiceServer
type echoServer struct{}
func newEchoServer() pb.EchoServiceServer {
return new(echoServer)
}
func (s *echoServer) Echo(ctx context.Context, msg *pb.Message) (*pb.Message, error) {
glog.Info(msg)
return msg, nil
}
func Run() error {
listen, err := net.Listen("tcp", ":50051")
if err != nil {
return err
}
server := grpc.NewServer()
pb.RegisterEchoServiceServer(server, newEchoServer())
server.Serve(listen)
return nil
}
func main() {
flag.Parse()
defer glog.Flush()
if err := Run(); err != nil {
glog.Fatal(err)
}
}
I want the server to listen from a Kinesis stream, so whenever a message is posted to the Kinesis stream, it will consume that message and send it to the Echo() method.
So, I'm envisioning something like:
func handleKinesis(s *echoServer) error {
// wait until a message is published to the kinesis stream
records, err := readFromKinesis() // not implemented here
if err != nil {
return err
}
for _, record := records {
message := string(record.Data)
_, _ = s.Echo(context.Background(), &pb.Message{msg: message})
}
}
And the handleKinesis would run in the background, just listening for new Kinesis messages, and then call the echoServer to process that message.
How can I accomplish that?
I a simple TCP server that accept connection , Get a URL using http.Get and then encode the response using gob. But gob fails to encode http.gzipReader.
On encoding it gives following error message:
gob: type not registered for interface: http.gzipReader
My code is given below:
package main
import (
"encoding/gob"
"fmt"
"log"
"net"
"net/http"
"os"
)
const (
CONN_HOST = "localhost"
CONN_PORT = "3333"
CONN_TYPE = "tcp"
)
func main() {
// Listen for incoming connections.
l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
for {
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
go handleRequest(conn)
}
}
func handleRequest(conn net.Conn) {
res, err :=
http.Get("http://info.cern.ch/hypertext/WWW/TheProject.html")
if err != nil {
log.Fatal(err)
} else {
encoder := gob.NewEncoder(conn)
er := encoder.Encode(res)
if er != nil {
fmt.Println(er)
}
}
conn.Write([]byte("Message received."))
conn.Close()
}
Is there any way to encode http.gzipReaderc ?? I saw an example of gob.Register() , but I am trying to registrer http.gzipReaderc . But unable to do so. can any one suggest me solution to this problem ?
I solved this issue by implementing my own ReadCloser. Check HTTP Caching Client
Theres a websocket running in my localhost on ws://localhost:8080/ws
I need go lang code that can create a websocket client and connect to this server.
My Google-Fu skills failed to teach me a simple way to do this.
Thank you.
Nevermind I found some helping code online. Now my code looks like this in case someone else needs this:
package main
import (
"net/http"
"text/template"
"code.google.com/p/go.net/websocket"
"fmt"
"os"
"time"
)
const address string = "localhost:9999"
func main() {
initWebsocketClient()
}
func initWebsocketClient() {
fmt.Println("Starting Client")
ws, err := websocket.Dial(fmt.Sprintf("ws://%s/ws", address), "", fmt.Sprintf("http://%s/", address))
if err != nil {
fmt.Printf("Dial failed: %s\n", err.Error())
os.Exit(1)
}
incomingMessages := make(chan string)
go readClientMessages(ws, incomingMessages)
i := 0
for {
select {
case <-time.After(time.Duration(2e9)):
i++
response := new(Message)
response.RequestID = i
response.Command = "Eject the hot dog."
err = websocket.JSON.Send(ws, response)
if err != nil {
fmt.Printf("Send failed: %s\n", err.Error())
os.Exit(1)
}
case message := <-incomingMessages:
fmt.Println(`Message Received:`,message)
}
}
}
func readClientMessages(ws *websocket.Conn, incomingMessages chan string) {
for {
var message string
// err := websocket.JSON.Receive(ws, &message)
err := websocket.Message.Receive(ws, &message)
if err != nil {
fmt.Printf("Error::: %s\n", err.Error())
return
}
incomingMessages <- message
}
}
Also as recoba suggested in the comment, there has been a new gorilla example here for the ones looking for a better solution.
Check out this event-based client, super easy: https://github.com/rgamba/evtwebsocket
Example:
package main
import (
"fmt"
"github.com/rgamba/evtwebsocket"
"golang.org/x/net/websocket"
)
func main() {
c := evtwebsocket.Conn{
OnConnected: func(w *websocket.Conn) {
fmt.Println("Connected")
},
OnMessage: func(msg []byte) {
fmt.Printf("Received message: %s\n", msg)
},
OnError: func(err error) {
fmt.Printf("** ERROR **\n%s\n", err.Error())
},
}
// Connect
c.Dial("ws://echo.websocket.org")
c.Send([]byte("TEST"), nil)
}
i have a problem with my golang server in which i'm using websockets.
The server opens the connection and the client could connect to it, but the problem is that when the server starts sending the data to the client, the client connection is closed after a small period of time. i suggest that the problem is with the server and not with the client because i tried to connect to the server with another web client, and it's the same issue. I didn't understand the cause ! Can someone help me?
server.go:
func Echo(ws *websocket.Conn) {
fmt.Println("Echoing")
for {
msg := MessageReceived{Name: "OrderCommand", Nbmsg: 3}
if err := websocket.JSON.Send(ws, msg); err != nil {
fmt.Println("Can't send")
break
}
//os.Exit(0)
}
}
func checkError(err error) {
if err != nil {
Log("Fatal error ", err.Error())
os.Exit(1)
}
}
func main() {
http.HandleFunc("/save", saveHandler)
http.Handle("/", websocket.Handler(Echo))
err:= http.ListenAndServe(":8081", nil)
checkError(err)
}
and client.go:
import (
"code.google.com/p/go.net/websocket"
"fmt"
"log"
)
func main() {
origin := "http://localhost/"
url := "ws://localhost:8081/echo"
ws, err := websocket.Dial(url, "", origin)
if err != nil {
log.Fatal(err)
}
var msg = make([]byte, 512)
var n int
if n, err = ws.Read(msg); err != nil {
log.Fatal(err)
}
fmt.Printf("Received: %s.\n", msg[:n])
}
Your problem, as others have pointed out, is that you must receive a message as well.
Currently, when someone connects, your program will step into the for-loop and start bombarding the client with messages. Probably not the intended behaviour of an echo server.
First you want to Receive a message, then Send a reply:
func Echo(ws *websocket.Conn) {
fmt.Println("Echoing")
msg := new(MessageReceived)
for {
// The server blocks here until a message from the client is received
websocket.JSON.Receive(ws, &msg)
fmt.Printf("Received message: %+v\n", msg)
// Reencode the same message and send it back
if err := websocket.JSON.Send(ws, msg); err != nil {
fmt.Println("Can't send echo")
break
}
}
}
A full working version can be found at Playground: http://play.golang.org/p/nQ3fJ5Nb0I
Since it uses websockets, you must compile it on your local computer.
Why using ws.Read when you can use websocket.JSON.Receive to deserialize the message?
Here are the server: http://play.golang.org/p/NZ6VJ4daGm
and the client: http://play.golang.org/p/rkJVKGhrGk (that I have changed to receive 10 messages before exiting).
The string "Can't send" will be printed by the server once the client closes the websocket connection.