Fiz*_*cus 12 javascript node.js socket.io
按照SocketIO网站上的文档,我有以下代码:
服务器
socket.on('foo', (arg, ack) => {
//Do stuff with arg
if(ack)
ack('response');
});
Run Code Online (Sandbox Code Playgroud)
客户
socket.emit('foo', arg, (response) => {
console.log(response);
});
Run Code Online (Sandbox Code Playgroud)
但是,ack从不调用该函数.事实上,确实如此undefined.我在服务器端和客户端都使用SocketIO v2.0.4.
我错过了什么吗?文档使它看起来应该很容易,但我无法理解它!
谢谢!
服务器端
var io = require('socket.io')(8090);
io.on('connection', function (socket) {
console.log('connected')
socket.on('ferret', function (arg, ack) {
console.log('ferret')
ack('woot');
});
});
Run Code Online (Sandbox Code Playgroud)
客户端
const ioClient = require('socket.io-client');
var client = ioClient.connect('http://localhost:8090');
client.emit('ferret', 'tobu', (response) => {
console.log(response)
console.log('ack')
});
Run Code Online (Sandbox Code Playgroud)
它将记录 ack() 和 'ack' 字符串的响应。我得到了参考socket.io 确认 node.js 示例。希望能帮助到你。
socket.io当版本与版本不匹配时,通常会出现此问题socket.io.js。这是我能够重现ack未定义的一种情况。
其他我刚刚用 Socket 2.04 测试过,它工作正常。
索引.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
var host = process.env.HOST || "0.0.0.0";
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.get('*', function (req, res) {
console.log(' request received ', req.headers.host);
res.status(200).send();
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
socket.on('ferret', function (name, fn) {
console.log('name', name, 'fn', fn);
fn('tarun');
});
});
http.listen(port,host, function(){
console.log('listening on *:' + port);
console.log('Env name is ' + process.env.name)
});
Run Code Online (Sandbox Code Playgroud)
索引.html
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
#messages { margin-bottom: 40px }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io();
socket.on('connect', function () { // TIP: you can avoid listening on `connect` and listen on events directly too!
socket.emit('ferret', 'tobi', function (data) {
console.log(data); // data will be 'woot'
});
});
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
window.scrollTo(0, document.body.scrollHeight);
});
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
包.json
{
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {
"express": "^4.15.2",
"socket.io": "^2.0.4"
},
"scripts": {
"start": "node index.js"
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
398 次 |
| 最近记录: |