【问题标题】:access golang websocket server with nodejs client使用 nodejs 客户端访问 golang websocket 服务器
【发布时间】:2013-02-02 16:51:46
【问题描述】:

我是 NodeJS 的新手。假设我有一个使用 Golang 的 websocket 包实现的回显服务器:

包主 进口 ( “code.google.com/p/go.net/websocket” “日志” “网络/http” ) func EchoServer(ws *websocket.Conn) { var 消息字符串 websocket.Message.Receive(ws, &msg) log.Printf("收到的消息:%s\n", msg) websocket.Message.Send(ws, msg) } 功能主要(){ http.Handle("/echo", websocket.Handler(EchoServer)) 错误 := http.ListenAndServe(":8082", nil) 如果错误!= nil { 恐慌(错误。错误()) } }

nodejs 客户端代码应该是什么样的?

【问题讨论】:

  • 你确定你的意思是 node.js 而不是浏览器 WebSockets?
  • 浏览器可以消费websocket服务。但我感兴趣的是一个独立的nodejs应用程序是否可以使用它。

标签: node.js websocket go


【解决方案1】:

作为 WebSocket-Node 库的作者,我可以向您保证,您无需为了不使用子协议而修改 WebSocket-Node 库代码。

上面的示例代码错误地显示为 connect() 函数的子协议参数传递了一个空字符串。如果您选择不使用子协议,则应将 JavaScript 的 null 值作为第二个参数传递,或者传递一个空数组(该库能够按需求降序向远程服务器建议多个支持的子协议),但不能为空字符串。

【讨论】:

  • 说真的,为什么这不是公认的答案?
  • @brian-mckelvey 谢谢你的评论。我需要 ws url 中的基本身份验证,怎么做? ws://f68a8efd-dc55-405d-b1f0-b4433477d52a:43093a7d-f563-43a7-8e46-56791cb1b92f@192.168.2.203:8011/v2/ws/websocket 试过这种方式,但得到以下错误。连接错误:错误:服务器以非 101 状态响应:401 响应标头遵循:www-authenticate:Basic realm="Authentication required" 日期:2016 年 9 月 8 日星期四 06:20:03 GMT 内容长度:25 内容-类型:文本/纯文本; charset=utf-8
  • 它对我有用,就像下面的方式 var auth = "Basic " + new Buffer(uname + ":" + pwd).toString("base64"); var headers = { "授权" : auth };
【解决方案2】:

我认为this 是您正在寻找的。使用您的服务器代码的快速示例:

var WebSocketClient = require('websocket').client;

var client = new WebSocketClient();

client.on('connectFailed', function(error) {
    console.log('Connect Error: ' + error.toString());
});

client.on('connect', function(connection) {
    console.log('WebSocket client connected');
    connection.on('error', function(error) {
        console.log("Connection Error: " + error.toString());
    });
    connection.on('close', function() {
        console.log('echo-protocol Connection Closed');
    });
    connection.on('message', function(message) {
        if (message.type === 'utf8') {
            console.log("Received: '" + message.utf8Data + "'");
        }
    });

    connection.sendUTF("Hello world");
});

client.connect('ws://127.0.0.1:8082/echo', "", "http://localhost:8082");

要使其正常工作,您需要修改lib/WebSocketCLient.js 中的WebsocketClient 代码。注释掉这些行(我机器上的第 299-300 行):

        //this.failHandshake("Expected a Sec-WebSocket-Protocol header.");
        //return;

由于某种原因,您提供的 websocket 库似乎没有发送“Sec-Websocket-Protocol”标头,或者至少客户端没有找到它。我没有做太多测试,但应该在某个地方提交错误报告。

这是一个使用 Go 客户端的示例:

package main

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

const message = "Hello world"

func main() {
    ws, err := websocket.Dial("ws://localhost:8082/echo", "", "http://localhost:8082")
    if err != nil {
        panic(err)
    }
    if _, err := ws.Write([]byte(message)); err != nil {
        panic(err)
    }
    var resp = make([]byte, 4096)
    n, err := ws.Read(resp)
    if err != nil {
        panic(err)
    }
    fmt.Println("Received:", string(resp[0:n]))
}

【讨论】:

  • 正确答案如下...如果您这样做,祝您升级成功
猜你喜欢
  • 2017-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-21
相关资源
最近更新 更多