我一直在尝试在Azure辅助角色中设置套接字服务器,但似乎没有任何工作。我试图让这个简单的教程起作用。这是server.js文件:
var port = process.env.port || 81;
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
app.listen(port);
console.log('socket.io server started on port: ' + port + '\n');
function handler (req, res) {
res.writeHead(200);
res.end('socket.io server started on port: ' + port + '\n');
}
// io.configure(function () {
// io.set("transports", ["xhr-polling"]);
// io.set("polling duration", 10);
// });
io.sockets.on('connection', function (socket) {
console.log('user connected');
socket.on('sendMessage', function(data){
console.log('user sent the message: ' + data.message + '\n');
socket.emit('helloBack', { message: 'Hello back!' });
});
});
使用以下客户端:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Socket Test</title>
<meta name="description" content="The azure socket tutorial">
<script src="https://cdn.socket.io/socket.io-1.3.4.js"></script>
<script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
<script type="text/javascript">
var socket;
$(document).ready(function () {
$("#startButton").click(function () {
$("#returnMessageLabel").empty();
if (!socket) {
socket = io.connect("http://127.0.0.1:80/");
socket.on('helloBack', function (data) {
$("#returnMessageLabel").text(data.message);
});
}
socket.emit('sendMessage', { message: 'Hello there!' });
});
});
</script>
</head>
<body>
<h1>Click to introduce yourself</h1>
<button id="startButton">Say Hello</button>
<label id="returnMessageLabel"></label>
</body>
</html>
它在端口80上的AzureEmulator中工作正常,但是当我发布角色时,我收到消息'socket.io server on port:80',但客户端无法连接以发送sendMessage。我从https://dzone.com/articles/windows-azure-web-worker-roles
获得了本教程以下是我的ServiceDefinition.csdef文件:
<?xml version="1.0" encoding="utf-16"?>
<ServiceDefinition xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="dzoned" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WorkerRole name="WorkerRole1">
<Startup>
<Task commandLine="setup_worker.cmd > log.txt" executionContext="elevated">
<Environment>
<Variable name="EMULATED">
<RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" />
</Variable>
<Variable name="RUNTIMEID" value="node" />
<Variable name="RUNTIMEURL" value="http://az413943.vo.msecnd.net/node/0.6.20.exe" />
</Environment>
</Task>
<Task commandLine="node.cmd .\startup.js" executionContext="elevated" />
</Startup>
<Endpoints>
<InputEndpoint name="HttpIn" protocol="tcp" port="80" />
<InputEndpoint name="SocketIn" protocol="http" port="81" />
</Endpoints>
<Runtime>
<Environment>
<Variable name="PORT">
<RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/Endpoints/Endpoint[@name='HttpIn']/@port" />
</Variable>
<Variable name="EMULATED">
<RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" />
</Variable>
</Environment>
<EntryPoint>
<ProgramEntryPoint commandLine="node.cmd .\server.js" setReadyOnProcessStart="true" />
</EntryPoint>
</Runtime>
</WorkerRole>
</ServiceDefinition>
答案 0 :(得分:0)
请尝试在云服务中配置辅助角色的端点。打开云服务项目中的ServiceDefinition.csdef
,在工作者角色定义下添加端点配置。
E.G。
<WorkerRole name="WorkerRole1" vmsize="Small">
<Endpoints>
<InputEndpoint name="HttpIn" protocol="http" port="81" />
</Endpoints>
...
</WorkerRole>
然后打包您的项目并部署到Azure。有关详细信息,请参阅https://azure.microsoft.com/en-us/documentation/articles/cloud-services-enable-communication-role-instances/。
任何更新,请随时告诉我。