使用Golang服务器直接创建WebSocket连接的静态HTML页面。

huangapple go评论123阅读模式
英文:

Static html page created the WebSocket connection golang server directly

问题

我正在编写一个需要创建到服务器的websocket的html页面。

在服务器上,我使用了code.google.com/p/go.net/websocket中的示例来接受连接。

然而,在Chrome26中,响应是

WebSocket连接到'ws://127.0.0.1:1234/'失败:意外的响应代码:400

有什么我漏掉的东西(比如握手)吗?

这是我的html,服务器使用的是go语言

  1. <html>
  2. <head></head>
  3. <body>
  4. <script type="text/javascript">
  5. var sock = null;
  6. var wsuri = "ws://127.0.0.1:1234";
  7. window.onload = function() {
  8. console.log("onload");
  9. sock = new WebSocket(wsuri);
  10. sock.onopen = function() {
  11. console.log("connected to " + wsuri);
  12. }
  13. sock.onclose = function(e) {
  14. console.log("connection closed (" + e.code + ")");
  15. }
  16. sock.onmessage = function(e) {
  17. console.log("message received: " + e.data);
  18. }
  19. };
  20. function send() {
  21. var msg = document.getElementById('message').value;
  22. sock.send(msg);
  23. };
  24. </script>
  25. <h1>WebSocket Echo Test</h1>
  26. <form>
  27. <p>
  28. Message: <input id="message" type="text" value="Hello, world!">
  29. </p>
  30. </form>
  31. <button onclick="send();">Send Message</button>
  32. </body>
  33. </html>
  1. package main
  2. import (
  3. "code.google.com/p/go.net/websocket"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. )
  8. func Echo(ws *websocket.Conn) {
  9. var err error
  10. for {
  11. var reply string
  12. if err = websocket.Message.Receive(ws, &reply); err != nil {
  13. fmt.Println("Can't receive")
  14. break
  15. }
  16. fmt.Println("Received back from client: " + reply)
  17. msg := "Received: " + reply
  18. fmt.Println("Sending to client: " + msg)
  19. if err = websocket.Message.Send(ws, msg); err != nil {
  20. fmt.Println("Can't send")
  21. break
  22. }
  23. }
  24. }
  25. func main() {
  26. http.Handle("/", websocket.Handler(Echo))
  27. if err := http.ListenAndServe(":1234", nil); err != nil {
  28. log.Fatal("ListenAndServe:", err)
  29. }
  30. }
英文:

I'm writing an html page that needs to create a websocket to the server

On the server, I used the example in "code.google.com/p/go.net/websocket" just accept the connection.

However, in Chrome26 the response is

WebSocket connection to 'ws://127.0.0.1:1234/' failed: Unexpected response code: 400

Is there something is missed (like a handshake)?

This is my html and server is using go

  1. &lt;html&gt;
  2. &lt;head&gt;&lt;/head&gt;
  3. &lt;body&gt;
  4. &lt;script type=&quot;text/javascript&quot;&gt;
  5. var sock = null;
  6. var wsuri = &quot;ws://127.0.0.1:1234&quot;;
  7. window.onload = function() {
  8. console.log(&quot;onload&quot;);
  9. sock = new WebSocket(wsuri);
  10. sock.onopen = function() {
  11. console.log(&quot;connected to &quot; + wsuri);
  12. }
  13. sock.onclose = function(e) {
  14. console.log(&quot;connection closed (&quot; + e.code + &quot;)&quot;);
  15. }
  16. sock.onmessage = function(e) {
  17. console.log(&quot;message received: &quot; + e.data);
  18. }
  19. };
  20. function send() {
  21. var msg = document.getElementById(&#39;message&#39;).value;
  22. sock.send(msg);
  23. };
  24. &lt;/script&gt;
  25. &lt;h1&gt;WebSocket Echo Test&lt;/h1&gt;
  26. &lt;form&gt;
  27. &lt;p&gt;
  28. Message: &lt;input id=&quot;message&quot; type=&quot;text&quot; value=&quot;Hello, world!&quot;&gt;
  29. &lt;/p&gt;
  30. &lt;/form&gt;
  31. &lt;button onclick=&quot;send();&quot;&gt;Send Message&lt;/button&gt;
  32. &lt;/body&gt;
  33. &lt;/html&gt;

//------------------------------

  1. package main
  2. import (
  3. &quot;code.google.com/p/go.net/websocket&quot;
  4. &quot;fmt&quot;
  5. &quot;log&quot;
  6. &quot;net/http&quot;
  7. )
  8. func Echo(ws *websocket.Conn) {
  9. var err error
  10. for {
  11. var reply string
  12. if err = websocket.Message.Receive(ws, &amp;reply); err != nil {
  13. fmt.Println(&quot;Can&#39;t receive&quot;)
  14. break
  15. }
  16. fmt.Println(&quot;Received back from client: &quot; + reply)
  17. msg := &quot;Received: &quot; + reply
  18. fmt.Println(&quot;Sending to client: &quot; + msg)
  19. if err = websocket.Message.Send(ws, msg); err != nil {
  20. fmt.Println(&quot;Can&#39;t send&quot;)
  21. break
  22. }
  23. }
  24. }
  25. func main() {
  26. http.Handle(&quot;/&quot;, websocket.Handler(Echo))
  27. if err := http.ListenAndServe(&quot;:1234&quot;, nil); err != nil {
  28. log.Fatal(&quot;ListenAndServe:&quot;, err)
  29. }
  30. }

答案1

得分: 5

Chrome可能会抛出400错误,因为它认为您正在尝试对websocket服务器进行跨域请求,并认为您可能没有权限。

要解决此问题,您只需从您的go服务器上提供html。

所以将您的sock.go代码更改为:

  1. package main
  2. import (
  3. "code.google.com/p/go.net/websocket"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. )
  8. func Echo(ws *websocket.Conn) {
  9. var err error
  10. for {
  11. var reply string
  12. if err = websocket.Message.Receive(ws, &reply); err != nil {
  13. fmt.Println("无法接收")
  14. break
  15. }
  16. fmt.Println("从客户端接收到的消息:" + reply)
  17. msg := "接收到的消息:" + reply
  18. fmt.Println("发送给客户端的消息:" + msg)
  19. if err = websocket.Message.Send(ws, msg); err != nil {
  20. fmt.Println("无法发送")
  21. break
  22. }
  23. }
  24. }
  25. func main() {
  26. http.Handle("/", http.FileServer(http.Dir("."))) // <-- 注意这一行
  27. http.Handle("/socket", websocket.Handler(Echo))
  28. log.Println("服务中")
  29. if err := http.ListenAndServe(":1234", nil); err != nil {
  30. log.Fatal("ListenAndServe:", err)
  31. }
  32. }

并将您的index.html文件添加到与sock.go文件相同的目录中:

  1. <html>
  2. <head></head>
  3. <body>
  4. <script type="text/javascript">
  5. var sock = null;
  6. var wsuri = "ws://127.0.0.1:1234/socket"; // <-- 注意新的路径
  7. window.onload = function() {
  8. console.log("onload");
  9. sock = new WebSocket(wsuri);
  10. sock.onopen = function() {
  11. console.log("已连接到" + wsuri);
  12. }
  13. sock.onclose = function(e) {
  14. console.log("连接已关闭(" + e.code + ")");
  15. }
  16. sock.onmessage = function(e) {
  17. console.log("收到消息:" + e.data);
  18. }
  19. };
  20. function send() {
  21. var msg = document.getElementById('message').value;
  22. sock.send(msg);
  23. };
  24. </script>
  25. <h1>WebSocket Echo测试</h1>
  26. <form>
  27. <p>
  28. 消息:<input id="message" type="text" value="Hello, world!">
  29. </p>
  30. </form>
  31. <button onclick="send();">发送消息</button>
  32. </body>
  33. </html>

现在您将能够在Chrome中进行连接。

英文:

Chrome is likely throwing error 400 because it thinks you are trying to do a cross-domain request to the websocket server and thinks it is unlikely you have permission.

To solve the issue you simply have to server your html from your go-server too.

So change your sock.go code to:

  1. package main
  2. import (
  3. &quot;code.google.com/p/go.net/websocket&quot;
  4. &quot;fmt&quot;
  5. &quot;log&quot;
  6. &quot;net/http&quot;
  7. )
  8. func Echo(ws *websocket.Conn) {
  9. var err error
  10. for {
  11. var reply string
  12. if err = websocket.Message.Receive(ws, &amp;reply); err != nil {
  13. fmt.Println(&quot;Can&#39;t receive&quot;)
  14. break
  15. }
  16. fmt.Println(&quot;Received back from client: &quot; + reply)
  17. msg := &quot;Received: &quot; + reply
  18. fmt.Println(&quot;Sending to client: &quot; + msg)
  19. if err = websocket.Message.Send(ws, msg); err != nil {
  20. fmt.Println(&quot;Can&#39;t send&quot;)
  21. break
  22. }
  23. }
  24. }
  25. func main() {
  26. http.Handle(&quot;/&quot;, http.FileServer(http.Dir(&quot;.&quot;))) // &lt;-- note this line
  27. http.Handle(&quot;/socket&quot;, websocket.Handler(Echo))
  28. log.Println(&quot;serving&quot;)
  29. if err := http.ListenAndServe(&quot;:1234&quot;, nil); err != nil {
  30. log.Fatal(&quot;ListenAndServe:&quot;, err)
  31. }
  32. }

and add your index.html file to the same directory as your sock.go file:

  1. &lt;html&gt;
  2. &lt;head&gt;&lt;/head&gt;
  3. &lt;body&gt;
  4. &lt;script type=&quot;text/javascript&quot;&gt;
  5. var sock = null;
  6. var wsuri = &quot;ws://127.0.0.1:1234/socket&quot;; // &lt;-- note new path
  7. window.onload = function() {
  8. console.log(&quot;onload&quot;);
  9. sock = new WebSocket(wsuri);
  10. sock.onopen = function() {
  11. console.log(&quot;connected to &quot; + wsuri);
  12. }
  13. sock.onclose = function(e) {
  14. console.log(&quot;connection closed (&quot; + e.code + &quot;)&quot;);
  15. }
  16. sock.onmessage = function(e) {
  17. console.log(&quot;message received: &quot; + e.data);
  18. }
  19. };
  20. function send() {
  21. var msg = document.getElementById(&#39;message&#39;).value;
  22. sock.send(msg);
  23. };
  24. &lt;/script&gt;
  25. &lt;h1&gt;WebSocket Echo Test&lt;/h1&gt;
  26. &lt;form&gt;
  27. &lt;p&gt;
  28. Message: &lt;input id=&quot;message&quot; type=&quot;text&quot; value=&quot;Hello, world!&quot;&gt;
  29. &lt;/p&gt;
  30. &lt;/form&gt;
  31. &lt;button onclick=&quot;send();&quot;&gt;Send Message&lt;/button&gt;
  32. &lt;/body&gt;
  33. &lt;/html&gt;

Now you will be able to connect from within chrome.

huangapple
  • 本文由 发表于 2013年4月9日 00:40:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/15884480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定