【问题标题】:Node.js, Websocket server-side client dataNode.js,Websocket 服务器端客户端数据
【发布时间】:2016-05-21 04:21:47
【问题描述】:

首先,我希望你明白我的需要。 这是我当前的服务器端脚本:

var server = require('websocket').server, http = require('http');
var mysql = require('mysql');

var socket = new server({
    httpServer: http.createServer().listen(8080)
});



var sql = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : ''
});

sql.connect(function(err) {
  if (err) {
    console.error('[MySQL] Error connecting: ' + err.stack);
    return;
  }
  console.log('[MySQL] Connected as id ' + sql.threadId);
});

socket.on('request', function(request) {
    var connection = request.accept(null, request.origin);

    connection.on('message', function(message) {
        connection.sendUTF(message);
    });

    connection.on('close', function(connection) {
        console.log('connection closed');
    });
}); 

客户端使用唯一查询连接到 websocket:

var WS = new WebSocket("ws://localhost:8080/steamid="+steamid);

现在是我需要的。 在服务器端,我需要获取客户端的 steamid 并保存,以便稍后在服务器中使用。例如,我需要获取 第 10 个客户的 Steamid。我相信这必须用数组来完成,但我对这些事情有点陌生。

提前致谢, 内达斯

【问题讨论】:

  • ws://localhost:8080/steamid=steamid 看起来不像是一个有效的网址。我猜应该是这样的:ws://localhost:8080/?steamid=steamid?您的问题是如何访问该查询参数?那么这个问题可能会有所帮助:stackoverflow.com/questions/17301269/…

标签: javascript node.js websocket server steam-web-api


【解决方案1】:

直接检查请求对象和parse连接URL(request.resourcerequest.httpRequest.url)或request.resourceURL的内容以检索steamid查询参数值。

关于第n个客户端的steamid:

你可以像这样访问 websocket 服务器的所有连接:

server.connections[10]

然后按上述进行;检索并解析连接 url 以找到 steamid 查询参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 2018-04-11
    • 2011-12-26
    • 1970-01-01
    • 2016-06-28
    相关资源
    最近更新 更多