【发布时间】:2016-06-09 03:01:25
【问题描述】:
我有一个很简单的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');
});
客户端使用socket 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/试试看
【问题讨论】:
标签: javascript websocket socket.io