[英]socket.io can't get it to work
EDIT 1:编辑 1:
Ok, problem with the server solved.好了,服务器问题解决了。 This is the full server code integrated into my project.
这是集成到我的项目中的完整服务器代码。
var http = require('http');
var express = require('express');
var requestHandler = require(__dirname + '/app_modules/request-handler.js');
var app = express();
app.configure(function(){
app.use(express.static(__dirname + '/html'));
app.use(express.bodyParser());
});
var httpServer = http.createServer(app);
httpServer.listen(80);
var io = require('socket.io').listen(httpServer);
io.on('connection', function(socket){
socket.on('event', function(data){
});
socket.on('disconnect', function(){
});
});
The next issue is including the source .js in my static html page.下一个问题是在我的静态 html 页面中包含源 .js。 This throws an error:
这会引发错误:
<script src="/socket.io/socket.io"></script>
Resource interpreted as Script but transferred with MIME type text/plain: "http://localhost/socket.io/socket.io". localhost/:206
Uncaught SyntaxError: Unexpected identifier socket.io:1
Uncaught ReferenceError: io is not defined
ORIGINAL PROBLEM:原问题:
I'm trying to get this sucker working on a test server.我试图让这个傻瓜在测试服务器上工作。 This is 1:1, straight from the socket.io docs on github.
这是 1:1,直接来自 github 上的 socket.io 文档。
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
io.on('connection', function(socket){
socket.on('event', function(data){
});
socket.on('disconnect', function(){
});
});
server.listen(3000);
Here is what I'm getting:这是我得到的:
[INFO] 19:37:40 Restarting
[ERROR] 19:37:40 TypeError
TypeError: object is not a function
at Object.<anonymous> (/Users/me/work/github/node-forever-gui/server/socket_test.js:3:30)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Object..js (/usr/local/lib/node_modules/node-dev/lib/hook.js:52:17)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/usr/local/lib/node_modules/node-dev/lib/wrap.js:47:1)
at Module._compile (module.js:456:26)
All modules are installed through npm .所有模块都通过npm安装。 Please help me figure this out!
请帮我解决这个问题!
The is exactly what is stated, io
is not a function and you can't pass a HTTP object to it.这正是所说的,
io
不是一个函数,您不能将 HTTP 对象传递给它。 Use the listen method instead to attach Socket.IO to a HTTP instance.改为使用 listen 方法将 Socket.IO 附加到 HTTP 实例。
var io = require('socket.io').listen(server);
For your second error, the file is a script, and has an extension.对于您的第二个错误,该文件是一个脚本,并且具有扩展名。 Add the
.js
to the reference.将
.js
添加到引用中。
<script src="/socket.io/socket.io.js"></script>
some people have this problem because the import in an es6 env was written wrong有些人有这个问题,因为 es6 env 中的导入写错了
My code when it was buggy我的代码有问题时
import { socketIo } from 'socket.io';
// start server on port
const server = app.server.listen(process.env.PORT || config.port, () => {
console.log(` ====== Listening on ${app.server.address().port} ====== `.bgWhite.black.bold);
});
// socketIo.io
const io = socketIo(server);
io.on('connection', (socket) => {
console.log(` ====== Socket Connected => `.bgWhite.black.bold, socket.id);
});
UnhandledPromiseRejectionWarning: TypeError: socket_io_1.socketIo is not a function
UnhandledPromiseRejectionWarning: TypeError: socket_io_1.socketIo 不是函数
fix was simple修复很简单
import * as socketIo from 'socket.io';
here is an example https://github.com/luixaviles/socket-io-typescript-chat/blob/master/server/src/chat-server.ts这是一个例子https://github.com/luixaviles/socket-io-typescript-chat/blob/master/server/src/chat-server.ts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.