栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

UE4 使用Websockets与Python通信

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

UE4 使用Websockets与Python通信

  1. 在UE4 的build.cs里面加上WebSocket

​

  1. UE4 里面新建一个GameInstance C++文件

xxGameInstance.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "IWebSocket.h"
#include "WebSocketGameInstance.generated.h"


UCLASS()
class WEBSOCKETDEMO_API UWebSocketGameInstance : public UGameInstance
{
	GENERATED_BODY()
public:
	virtual void Init() override;
	virtual void Shutdown() override;
	TSharedPtr WebSocket;
	
};

xxGameInstance.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "WebSocketGameInstance.h"
#include "WebSocketsModule.h"

void UWebSocketGameInstance::Init() {
	Super::Init();

	if (!FModuleManager::Get().IsModuleLoaded("WebSockets")) {
		FModuleManager::Get().LoadModule("WebSockets");
	}

	WebSocket = FWebSocketsModule::Get().CreateWebSocket("ws://127.0.0.1:7890");

	WebSocket->OnConnected().AddLambda([]() {
		GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green, "Successfully Connected");
	});
	WebSocket->OnMessage().AddLambda([](const FString& MessageString) {
		GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, "Received Message: " + MessageString);
	});

	WebSocket->OnMessageSent().AddLambda([](const FString& MessageString) {
		GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, "Sent Message: " + MessageString);
	});

	WebSocket->Connect();

}

void UWebSocketGameInstance::Shutdown() {

	if (WebSocket->IsConnected()) {
		WebSocket->Close();
	}

	Super::Shutdown();
}
  1. 调用Send函数
void AWebsocketDemoCharacter::NotifyServer() {
	UWebSocketGameInstance* GameInstance = Cast(GetGameInstance());
	if (GameInstance) {
		if (GameInstance->WebSocket->IsConnected()) {
			GameInstance->WebSocket->Send("FROM UnrealEngine 4");
		}
	}
}
void AWebsocketDemoCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
	// Set up gameplay key bindings
	check(PlayerInputComponent);
	PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
	PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);

	PlayerInputComponent->BindAxis("MoveForward", this, &AWebsocketDemoCharacter::MoveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &AWebsocketDemoCharacter::MoveRight);

	// We have 2 versions of the rotation bindings to handle different kinds of devices differently
	// "turn" handles devices that provide an absolute delta, such as a mouse.
	// "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
	PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
	PlayerInputComponent->BindAxis("TurnRate", this, &AWebsocketDemoCharacter::TurnAtRate);
	PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
	PlayerInputComponent->BindAxis("LookUpRate", this, &AWebsocketDemoCharacter::LookUpAtRate);

	// handle touch devices
	PlayerInputComponent->BindTouch(IE_Pressed, this, &AWebsocketDemoCharacter::TouchStarted);
	PlayerInputComponent->BindTouch(IE_Released, this, &AWebsocketDemoCharacter::TouchStopped);

	// VR headset functionality
	PlayerInputComponent->BindAction("ResetVR", IE_Pressed, this, &AWebsocketDemoCharacter::OnResetVR);
	PlayerInputComponent->BindAction("NotifyServer", IE_Pressed, this, &AWebsocketDemoCharacter::NotifyServer);

}
  1. 在UE4 里面将GameInstance更改成写好的

  1. Python代码
import websockets
import asyncio
import json
global data
PORT = 7890
print("Running on PORT 7890")
async def echo(websocket,path):
    global data
    print("A client just connected")
    async for message in websocket:
        print(message)
        await websocket.send("From Python")

start_server = websockets.serve(echo,"127.0.0.1",PORT)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1026300.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号