Missing error handler on `socket`. TypeError: socket.set is not a function
来源:互联网 发布:淘宝刷到单流程视频 编辑:程序博客网 时间:2024/06/01 09:33
在学习socket.io时候遇到了下面这个问题:
服务器端代码如下:
var express = require('express');var http = require('http');var sio = require('socket.io');var app = express();var server = http.createServer(app);app.get('/',function(req,res){ res.sendFile(__dirname+'/index.html');});server.listen(3000,function(){ console.log("listening on port 3000");});var socket = sio.listen(server);socket.on('connection',function(socket){ socket.on('set nickName',function(name){ socket.set('nickName',name,function(){ socket.emit('send nickName',name); }); }); socket.on('get nickName',function(){ socket.get('nickName',function(err,name){ if(err){ socket.emit('err',err.message); }else{ socket.emit('send nickName',name); } }); });})
客户端代码如下:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>chatroom</title></head><body> <input type="text" id="nickname"> <input type="button" onclick="setNickName()" value="设置昵称"> <input type="button" onclick="getNickName()" value="获取昵称"> <script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect(); function setNickName(){ socket.emit('set nickName',document.getElementById("nickname").value); } function getNickName(){ socket.emit('get nickName'); } socket.on('send nickName',function(data){ console.log("My name is "+data); }); socket.on('err',function(data){ console.log('服务器出现错误:'+data); }) </script></body></html>
最后报错如上,找到原因是
socket.io中的get / set方法在新版本(1.x)中已经被抛弃了。
一种替代方法是利用socket对象。像下面这样。
socket.on(‘set nickname’, function (name) {
socket.nickname = name;
});
0 0
- Missing error handler on `socket`. TypeError: socket.set is not a function
- TypeError: io.sockets.socket is not a function 解决方案
- Uncaught TypeError: $(...).on is not a function
- Uncaught TypeError: s.elem.on is not a function
- 奇怪的TypeError: Error #1006: getInstance is not a function.
- TypeError: Error #1006: dispatchEvent is not a function
- Uncaught TypeError: $.ajax(...).error is not a function
- typeError: form.submit is not a function
- Uncaught TypeError: undefined is not a function
- Uncaught TypeError: string is not a function
- TypeError: e[h] is not a function
- Uncaught TypeError: object is not a function
- TypeError: jQuery(...).validationEngine is not a function
- TypeError: $(...).ajaxSubmit is not a function
- Uncaught TypeError: undefined is not a function
- Uncaught TypeError: undefined is not a function
- Uncaught TypeError: object is not a function
- Uncaught TypeError: boolean is not a function
- 第三方服务整理 (更新中)
- SpringData 学习(5)—— 使用 @Modifying 注解完成修改操作
- 字符串匹配算法分析--BF和KMP算法
- Python练习2 - 廖雪峰教程
- SVN服务器更换IP地址,客户端如何重新定位
- Missing error handler on `socket`. TypeError: socket.set is not a function
- Part 3 How to Tune Java Garbage Collection
- 番外篇-程序员和编译器之间的暧昧
- 三种 session存储方式
- C语言找素数
- 水平和垂直居中
- bzoj 1415 聪聪和可可 【期望】
- #ifndef/#define/#endif使用详解
- [期望 生成函数 卷积 导数] BZOJ 4001 [TJOI2015]概率论