程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

UE4 使用Websockets與Python通信

編輯: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<IWebSocket> 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<UWebSocketGameInstance>(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()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved