英文:
access golang websocket server with nodejs client
问题
以下是Node.js客户端代码的示例:
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost:8082/echo');
ws.on('open', function open() {
ws.send('Hello, server!');
});
ws.on('message', function incoming(data) {
console.log('Message received: ' + data);
});
ws.on('close', function close() {
console.log('Connection closed');
});
请注意,您需要先安装ws
模块,可以使用以下命令进行安装:
npm install ws
英文:
I am a newbie to NodeJS. Assume that I have a echo server implemented with Golang's websocket package:
<pre>
package main
import (
"code.google.com/p/go.net/websocket"
"log"
"net/http"
)
func EchoServer(ws *websocket.Conn) {
var msg string
websocket.Message.Receive(ws, &msg)
log.Printf("Message Got: %s\n", msg)
websocket.Message.Send(ws, msg)
}
func main() {
http.Handle("/echo", websocket.Handler(EchoServer))
err := http.ListenAndServe(":8082", nil)
if err != nil {
panic(err.Error())
}
}
</pre>
What should the nodejs client code look like ?
答案1
得分: 23
作为WebSocket-Node库的作者,我可以向您保证,您无需修改WebSocket-Node库的代码来避免使用子协议。
上面的示例代码错误地显示了在connect()函数的subprotocol参数中传递空字符串。如果您选择不使用子协议,您应该将JavaScript的null值作为第二个参数传递,或者传递一个空数组(该库能够按降序推荐多个支持的子协议给远程服务器),但不要传递空字符串。
英文:
As the author of the WebSocket-Node library, I can assure you that you do not need to modify the WebSocket-Node library code in order to not use a subprotocol.
The example code above incorrectly shows passing an empty string for the subprotocol parameter of the connect() function. If you are choosing not to use a subprotocol, you should pass JavaScript's null value as the second parameter, or an empty array (the library is able to suggest multiple supported subprotocols to the remote server in order of descending desirability), but not an empty string.
答案2
得分: 10
我认为这个是你在寻找的东西。使用你的服务器代码的快速示例:
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]))
}
英文:
I think this is what you're looking for. Quick example using your server code:
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");
To get this to work, you'll need to modify the WebsocketClient code in lib/WebSocketCLient.js
. Comment these lines out (lines 299-300 on my machine):
//this.failHandshake("Expected a Sec-WebSocket-Protocol header.");
//return;
For some reason the websocket library you provided doesn't seem to send the "Sec-Websocket-Protocol" header, or at least the client doesn't find it. I haven't done too much testing, but a bug report should probably be filed somewhere.
Here's an example using a Go client:
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]))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论