【发布时间】:2013-12-22 21:00:54
【问题描述】:
我正在尝试使用 typescript 创建从服务器到客户端的 socket.io 连接,为此我需要在构造函数中定义“套接字”对象,以便该对象可以使用套接字向客户端发送数据。但是,套接字对象在编译为 javascript 时不会被定义为该类的变量,这会阻止它被调用。
这是打字稿。
导出类上传{
directory = "./data";
socket: Socket;
constructor(socket:Socket) {
this.socket = socket;
}
public upload(req:express.Request, res) {
this.socket.emit('fileReceivedConfirmation', {
successful:true,
filePath: filePath,
originalName: originalFileName
});
}
这是对应的javascript
function Upload(socket) {
this.directory = "./data";
this.socket = socket;
}
Upload.prototype.upload = function (req, res) {
this.socket.emit('fileReceivedConfirmation', {
successful: true,
filePath: filePath,
originalName: originalFileName
});
}
socket 变量没有在编译后的 Javascript 中定义——当函数调用 socket 时会产生这个错误。
C:\Users\Me\WebstormProjects\Core\lib\Upload.js:43
this.socket.emit('fileReceivedConfirmation', {
^
TypeError: Cannot call method 'emit' of undefined
【问题讨论】:
标签: socket.io typescript