0

“使用socket.io,我想将用户发送的消息仅广播给所有客户端的一个子集。该子集将位于用户发出消息的一定距离内(比如1公里)。看来我需要将类似这样的内容(http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points)添加到 [socket.on 'broadcast', (消息) ] 下面的代码部分(请注意这是咖啡脚本,取自 Shoaib Burq 的 geochat-example github repo (https://github.com/sabman/geochat-example))。

现在包括里卡多的建议,用他的输入替换旧的 [socket.on 'broadcast', (message) ->] 部分(尽管我将“用户”更改为“记录”以与 server.coffee 代码的其余部分保持一致) ,以及末尾的 [find:] 部分。不过,出了点问题,我不确定它是否与 html 代码或咖啡脚本的更新有关。再次提前感谢..

代码仍然没有运行。包括里卡多的最新建议:

 socket.on 'broadcast', (message) ->
    # find the sender and extract it's position
    Character.find { clientId: socket.id }, (record) =>
        chat_data =
            user: record
            conversation: message
        [lat, lng] = record.geometry.coordinates
        # find everyone within a 1km square
        km = 1/111
         area = 
            type: 'Polygon'
            coordinates: [
               [lat - km, lng - km]
               [lat + km, lng - km]
               [lat + km, lng + km]
               [lat - km, lng + km]
            ]
        Character.find { within: area }, (record) ->
            // send message to each user

     socket.broadcast.send JSON.stringify(chat_data)

我感觉问题可能出在“查找”部分。例如,李嘉图写道

    console.log ">> find #{JSON.stringify(attrs)}"

但在原始代码中,这读起来是“=>”而不是“>>”:

    console.log "=> find #{JSON.stringify(attrs)}"

qs = require 'querystring'

find: (query, callback) ->
    console.log ">> find #{JSON.stringify(attrs)}"

    q = { operator: "or",  properties: attrs }
    url = lyr_config_characters.api_url
    key = lyr_config_characters.acl.get

    if query.id?
      url += "/#{attrs.id}?" + qs.stringify({ key })
    else if query.within?
      url += "/functions/within?" + qs.stringify({ key, input : query.within })
    else
      url += qs.stringify { key, input: q }

    req =
        method: "GET"
        uri: url
        headers: { "Content-Type": "application/json" }

    request req, (error, response, body) ->
        console.log error if error
        console.log "<< find #{body}"
        callback JSON.parse body ? '{}'

谢谢!

4

1 回答 1

0

您使用的示例基于spatialdb,它的文档似乎非常不完整。

从外观上看,您需要使用几何对象查询用户,它将返回包含在其中的点(用户):

socket.on 'broadcast', (message) ->
    # find the sender and extract it's position
    Character.find { clientId: socket.id }, (user) ->
        chat_data =
            user: user
            conversation: message
        [lat, lng] = user.geometry.coordinates
        # find everyone within a 1km square
        km = 1/111
        area = 
            type: 'Polygon'
            coordinates: [
               [lat - km, lng - km]
               [lat + km, lng - km]
               [lat + km, lng + km]
               [lat - km, lng + km]
            ]
        Character.find { within: area }, (users) ->
            // send message to each user

您还需要修改find方法以使用within查询:

find: (attrs, callback) ->
    console.log "=> find #{JSON.stringify(attrs)}"
    q = { operator: "or",  properties: attrs }
    if _.contains(_.keys(attrs), "id")
      url = "#{lyr_config_characters.api_url}/#{attrs.id}?key=#{lyr_config_characters.acl.get}"
    else if _.contains(_.keys(attrs), "within")
      url = "#{lyr_config_characters.api_url}/functions/within?key=#{lyr_config_characters.acl.get}&input=#{encodeURIComponent(JSON.stringify(attrs.within))}"
    else
      url = "#{lyr_config_characters.api_url}?key=#{lyr_config_characters.acl.get}&input=#{encodeURIComponent(JSON.stringify(q))}"
    req = { method: "GET", uri: url, headers: {"Content-Type": "application/json"} }

(为了理智,我会重写)

qs = require 'querystring'

find: (query, callback) ->
    console.log ">> find #{JSON.stringify(attrs)}"

    q = { operator: "or",  properties: attrs }
    url = lyr_config_characters.api_url
    key = lyr_config_characters.acl.get

    if query.id?
      url += "/#{attrs.id}?" + qs.stringify({ key })
    else if query.within?
      url += "/functions/within?" + qs.stringify({ key, input : query.within })
    else
      url += qs.stringify { key, input: q }

    req =
        method: "GET"
        uri: url
        headers: { "Content-Type": "application/json" }

    request req, (error, response, body) ->
        console.log error if error
        console.log "<< find #{body}"
        callback JSON.parse body ? '{}'
于 2012-04-12T22:38:29.423 回答