6
我有一個非常簡單的socket.io聊天例如,服務器端代碼是這樣的:如何從ws客戶端連接到socket.io?
https://github.com/js-demos/socketio-chat-demo/blob/master/index.js
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static('public'));
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
通過io代碼插座連接它的客戶端和運作良好:
https://github.com/js-demos/socketio-chat-demo/blob/master/public%2Findex.html
<script>
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
但我想使用一些其他的WebSocket客戶端連接ŧ他的服務器,說,wscat
:
npm install -g wscat
wscat ws://localhost:3000
但它無法連接,與此錯誤:
error: Error: socket hang up
我的網址ws://localhost:3000
是錯的?如何使它工作?
PS:你可以看到這個項目https://github.com/js-demos/socketio-chat-demo/和嘗試