示例#1
0
v8::Handle<v8::Value> V8WebSocket::sendCallback(const v8::Arguments& args)
{
    INC_STATS("DOM.WebSocket.send()");

    if (!args.Length())
        return throwError("Not enough arguments", V8Proxy::SyntaxError);

    WebSocket* webSocket = V8WebSocket::toNative(args.Holder());
    v8::Handle<v8::Value> message = args[0];
    ExceptionCode ec = 0;
    bool result;
    if (V8ArrayBuffer::HasInstance(message)) {
        ArrayBuffer* arrayBuffer = V8ArrayBuffer::toNative(v8::Handle<v8::Object>::Cast(message));
        ASSERT(arrayBuffer);
        result = webSocket->send(arrayBuffer, ec);
    } else if (V8Blob::HasInstance(message)) {
        Blob* blob = V8Blob::toNative(v8::Handle<v8::Object>::Cast(message));
        ASSERT(blob);
        result = webSocket->send(blob, ec);
    } else {
        v8::TryCatch tryCatch;
        v8::Handle<v8::String> stringMessage = message->ToString();
        if (tryCatch.HasCaught())
            return throwError(tryCatch.Exception());
        result = webSocket->send(toWebCoreString(stringMessage), ec);
    }
    if (ec)
        return throwError(ec);

    return v8Boolean(result);
}
bool js_cocos2dx_extension_WebSocket_send(JSContext *cx, uint32_t argc, jsval *vp)
{
    JS::CallArgs argv = JS::CallArgsFromVp(argc, vp);
    JS::RootedObject obj(cx, argv.thisv().toObjectOrNull());
    js_proxy_t *proxy = jsb_get_js_proxy(obj);
    WebSocket* cobj = (WebSocket *)(proxy ? proxy->ptr : NULL);
    JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");

    if(argc == 1)
    {
        if (argv[0].isString())
        {
            ssize_t len = JS_GetStringLength(argv[0].toString());
            std::string data;
            jsval_to_std_string(cx, argv[0], &data);

            if (data.empty() && len > 0)
            {
                CCLOGWARN("Text message to send is empty, but its length is greater than 0!");
                //FIXME: Note that this text message contains '0x00' prefix, so its length calcuted by strlen is 0.
                // we need to fix that if there is '0x00' in text message,
                // since javascript language could support '0x00' inserted at the beginning or the middle of text message
            }

            cobj->send(data);
        }
        else if (argv[0].isObject())
        {
            uint8_t *bufdata = NULL;
            uint32_t len = 0;

            JS::RootedObject jsobj(cx, argv[0].toObjectOrNull());
            if (JS_IsArrayBufferObject(jsobj))
            {
                bufdata = JS_GetArrayBufferData(jsobj);
                len = JS_GetArrayBufferByteLength(jsobj);
            }
            else if (JS_IsArrayBufferViewObject(jsobj))
            {
                bufdata = (uint8_t*)JS_GetArrayBufferViewData(jsobj);
                len = JS_GetArrayBufferViewByteLength(jsobj);
            }

            cobj->send(bufdata, len);
        }
        else
        {
            JS_ReportError(cx, "data type to be sent is unsupported.");
            return false;
        }

        argv.rval().setUndefined();

        return true;
    }
    JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 0);
    return false;
}
示例#3
0
JSBool js_cocos2dx_extension_WebSocket_send(JSContext *cx, uint32_t argc, jsval *vp)
{
	jsval *argv = JS_ARGV(cx, vp);
	JSObject *obj = JS_THIS_OBJECT(cx, vp);
	js_proxy_t *proxy = jsb_get_js_proxy(obj);
	WebSocket* cobj = (WebSocket *)(proxy ? proxy->ptr : NULL);
	JSB_PRECONDITION2( cobj, cx, JS_FALSE, "Invalid Native Object");

	if(argc == 1){
        do
        {
            if (JSVAL_IS_STRING(argv[0]))
            {
                std::string data;
                jsval_to_std_string(cx, argv[0], &data);
                cobj->send(data);
                break;
            }

            if (argv[0].isObject())
            {
                uint8_t *bufdata = NULL;
                uint32_t len = 0;
                
                JSObject* jsobj = JSVAL_TO_OBJECT(argv[0]);
                if (JS_IsArrayBufferObject(jsobj))
                {
                    bufdata = JS_GetArrayBufferData(jsobj);
                    len = JS_GetArrayBufferByteLength(jsobj);
                }
                else if (JS_IsArrayBufferViewObject(jsobj))
                {
                    bufdata = (uint8_t*)JS_GetArrayBufferViewData(jsobj);
                    len = JS_GetArrayBufferViewByteLength(jsobj);
                }
                
                if (bufdata && len > 0)
                {
                    cobj->send(bufdata, len);
                    break;
                }
            }
            
            JS_ReportError(cx, "data type to be sent is unsupported.");

        } while (0);
        
		JS_SET_RVAL(cx, vp, JSVAL_VOID);

		return JS_TRUE;
	}
	JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 0);
	return JS_TRUE;
}
bool js_cocos2dx_extension_WebSocket_send(JSContext *cx, uint32_t argc, jsval *vp)
{
    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
    JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
    js_proxy_t *proxy = jsb_get_js_proxy(obj);
    WebSocket* cobj = (WebSocket *)(proxy ? proxy->ptr : NULL);
    JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");

    if(argc == 1){
        do
        {
            if (args.get(0).isString())
            {
                std::string data;
                jsval_to_std_string(cx, args.get(0), &data);
                cobj->send(data);
                break;
            }

            if (args.get(0).isObject())
            {
                uint8_t *bufdata = NULL;
                uint32_t len = 0;
                
                JSObject* jsobj = args.get(0).toObjectOrNull();
                if (JS_IsArrayBufferObject(jsobj))
                {
                    bufdata = JS_GetArrayBufferData(jsobj);
                    len = JS_GetArrayBufferByteLength(jsobj);
                }
                else if (JS_IsArrayBufferViewObject(jsobj))
                {
                    bufdata = (uint8_t*)JS_GetArrayBufferViewData(jsobj);
                    len = JS_GetArrayBufferViewByteLength(jsobj);
                }
                
                if (bufdata && len > 0)
                {
                    cobj->send(bufdata, len);
                    break;
                }
            }
            
            JS_ReportError(cx, "data type to be sent is unsupported.");

        } while (0);
        
        args.rval().setUndefined();

        return true;
    }
    JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 0);
    return true;
}
示例#5
0
//! [processBinaryMessage]
void EchoServer::processBinaryMessage(QByteArray message)
{
	WebSocket *pClient = qobject_cast<WebSocket *>(sender());
	if (pClient != 0)
	{
		pClient->send(message);
	}
}
示例#6
0
//! [processMessage]
void EchoServer::processMessage(QString message)
{
	WebSocket *pClient = qobject_cast<WebSocket *>(sender());
	if (pClient != 0)
	{
		pClient->send(message);
	}
}
示例#7
0
void SIOClientImpl::heartbeat(float dt)
{
	std::string s = "2::";

	_ws->send(s);

	log("Heartbeat sent");
}
示例#8
0
void SIOClientImpl::connectToEndpoint(const std::string& endpoint)
{
	std::string path = endpoint == "/" ? "" : endpoint;

	std::string s = "1::" + path;

	_ws->send(s);
}
示例#9
0
	/**
	 * @author John M. Harris, Jr.
	 * @internal
	 */
	int WebSocket::lua_send(lua_State* L){
		WebSocket* LuaWebSocket = checkWebSocket(L, 1);
		if(LuaWebSocket != NULL){
			const char* msg = luaL_checkstring(L, 2);
			LuaWebSocket->send(msg);
		}
		return 0;
	}
示例#10
0
void InterfaceHttp::webSocketsProcessMessage(const QString &message) {
    WebSocket *webSocket = qobject_cast<WebSocket *>(sender());
    if(webSocket) {
        QStringList commandItems = message.split(COMMAND_END, QString::SkipEmptyParts);;
        QString response;
        foreach(const QString & command, commandItems)
            response += MessageManager::incomingMessage(MessageIncomming("http", webSocket->peerAddress().toString(), webSocket->peerPort(), "", command, command.split(" ", QString::SkipEmptyParts)), true, (command != "goto"));
        if(!response.isEmpty())
            webSocket->send(response);
    }
}
示例#11
0
void SIOClientImpl::emit(std::string endpoint, std::string eventname, std::string args)
{
	std::stringstream pre;

	std::string path = endpoint == "/" ? "" : endpoint;
	
	pre << "5::" << path << ":{\"name\":\"" << eventname << "\",\"args\":" << args << "}";

	std::string msg = pre.str();

	log("emitting event with data: %s", msg.c_str());

	_ws->send(msg);
}
示例#12
0
void SIOClientImpl::send(std::string endpoint, std::string s)
{
	std::stringstream pre;

	std::string path = endpoint == "/" ? "" : endpoint;
	
	pre << "3::" << path << ":" << s;

	std::string msg = pre.str();

	log("sending message: %s", msg.c_str());

	_ws->send(msg);
}
示例#13
0
void SIOClientImpl::disconnect()
{
	if(_ws->getReadyState() == WebSocket::State::OPEN)
    {
		std::string s = "0::";

		_ws->send(s);

		log("Disconnect sent");

		_ws->close();
	}

	Director::getInstance()->getScheduler()->unscheduleAllForTarget(this);

	_connected = false;

	SocketIO::getInstance()->removeSocket(_uri);
}
示例#14
0
EncodedJSValue JSC_HOST_CALL jsWebSocketPrototypeFunctionSend(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    if (!thisValue.inherits(&JSWebSocket::s_info))
        return throwVMTypeError(exec);
    JSWebSocket* castedThis = static_cast<JSWebSocket*>(asObject(thisValue));
    WebSocket* imp = static_cast<WebSocket*>(castedThis->impl());
    if (exec->argumentCount() < 1)
        return JSValue::encode(jsUndefined());
    ExceptionCode ec = 0;
    const String& data(ustringToString(exec->argument(0).toString(exec)));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());


    JSC::JSValue result = jsBoolean(imp->send(data, ec));
    setDOMException(exec, ec);
    return JSValue::encode(result);
}
示例#15
0
void SIOClientImpl::disconnectFromEndpoint(const std::string& endpoint)
{
	_clients.erase(endpoint);

	if (_clients.empty() || endpoint == "/")
    {
		log("SIOClientImpl::disconnectFromEndpoint out of endpoints, checking for disconnect");
		
		if(_connected)
            this->disconnect();
	}
    else
    {
		std::string path = endpoint == "/" ? "" : endpoint;

		std::string s = "0::" + path;

		_ws->send(s);
	}
}
示例#16
0
int main( int argc, const char** argv )
{

  int camera_number = 0;
  char socket_url[512];
  TopCodeScanner scanner;
  WebSocket *socket = NULL;

  if (argc < 2) {
    cerr << "expected: " << argv[0] << " <camera_number> [socket server]" << endl;
    cerr << "    example: > topcodes 0 ws://localhost:8126/topcodes" << endl;
    return -1;
  }

  // get the camera number
  camera_number = atoi(argv[1]);  // 0 if error

  if (argc >= 3) {
    socket = WebSocket::from_url(argv[2]);
  } else {
    socket = WebSocket::from_url("ws://localhost:8126/topcodes");
  }

  // open the default camera  
  VideoCapture cap(camera_number); 
  if (!cap.isOpened()) {
    cerr << "Error: Unable to open webcam " << camera_number << endl;
    return -1;
  }
 
  for(;;)
  {
    Mat frame, grey, flipped;

    // capture the next still video frame
    cap >> frame; 

    // flip the image horizontally so that it gives you a mirror reflection
    flip(frame, flipped, 1);

    // convert to greyscale
    cvtColor(flipped, grey, CV_RGB2GRAY);

    // scan for topcodes
    vector<TopCode*> *codes = scanner.scan(grey);

    // send topcode info through the websocket
    if (socket) {
      string json = "[\n";
      for (int i=0; i<codes->size(); i++) {
        TopCode *code = (*codes)[i];
        json += ("   " + code->toJSON() + ",\n");
      }
      json += "]";
      socket->send(json);
      socket->poll();
      socket->dispatch(handle_message);
    }

    // show the resulting image (debuggin)
    imshow("webcam", grey);

    // press the 'q' key to quit
    if (waitKey(30) >= 0) break;
  }

  if (socket) delete socket;
}