简体   繁体   English

使用NodeJ的Azure WebSocket

[英]Azure WebSocket using NodeJs

This code is working in Azure 该代码在Azure中运行

var http = require('http');
var port = process.env.port || 1337;
http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello World\n');
}).listen(port);

whereas the code is written below throws error '.azurewebsites.net is currently unable to handle this request. 而下面编写的代码将引发错误“ .azurewebsites.net当前无法处理此请求。 HTTP ERROR 500'. HTTP错误500'。 In Application logs it shows "Application has thrown an uncaught exception and is terminated: SyntaxError: Use of const in strict mode." 在应用程序日志中,它显示“应用程序引发了未捕获的异常并被终止:SyntaxError:在严格模式下使用const”。 Please suggest what could be the problem. 请提出可能是什么问题。 Currently using node version 9.2.0 also tried changing the version to 8.11.3 in both package.json as well as application settings. 当前使用的节点版本9.2.0还尝试在package.json和应用程序设置中将版本更改为8.11.3。

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

var port = process.env.PORT || 1337;
app.listen(port);

function handler(req, res) {
    fs.readFile(__dirname + '/page.html',
        function (err, data) {
            if (err) {
                res.writeHead(500);
                return res.end('Error loading index.html');
            }

            res.writeHead(200);
            res.end(data);
        });
}

io.on('connection', function (socket) {
    console.log((new Date()) + ' Connected to server socket');

    socket.emit('message', {
        msg: 'Connected! Greetings from server!'
    });

    socket.on('message', function (data) {
        console.log((new Date()) + ' Message: ' + data);
        socket.emit('message', {
            msg: 'Message received from client: ' + data
        });
    });

    socket.on('disconnect', function () {
        console.log((new Date()) + ' Disconnected!');
    });
});

Currently, Node of those two versions(8.11.3/9.2.0) are not available on Azure. 当前,这两个版本(8.11.3 / 9.2.0)的Node在Azure上不可用。

Once we specify a version not installed on Azure, an old version 0.10.40 is in use, where const is not enabled by default so that we met SyntaxError: Use of const in strict mode . 一旦指定了未在Azure上安装的版本,便会使用旧版本0.10.40,默认情况下未启用const ,因此我们遇到了SyntaxError: Use of const in strict mode See related thread for more details. 有关更多详细信息,请参见相关线程

We can use 10.0.0, 8.11.1, etc. Go to https://<yourwebappname>.scm.azurewebsites.net/api/diagnostics/runtime to see all versions available. 我们可以使用https://<yourwebappname>.scm.azurewebsites.net/api/diagnostics/runtime等。请访问https://<yourwebappname>.scm.azurewebsites.net/api/diagnostics/runtime以查看所有可用版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM